<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>array_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_array_array_test.js.html">array_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.arrayTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.arrayTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line23"></a>
<a name="line24"></a>function testArrayIndexOf() {
<a name="line25"></a>  assertEquals(goog.array.indexOf([0, 1, 2, 3], 1), 1);
<a name="line26"></a>  assertEquals(goog.array.indexOf([0, 1, 1, 1], 1), 1);
<a name="line27"></a>  assertEquals(goog.array.indexOf([0, 1, 2, 3], 4), -1);
<a name="line28"></a>  assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, 1), 1);
<a name="line29"></a>  assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, 2), -1);
<a name="line30"></a>  assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, -3), 1);
<a name="line31"></a>  assertEquals(goog.array.indexOf([0, 1, 2, 3], 1, -2), -1);
<a name="line32"></a>}
<a name="line33"></a>
<a name="line34"></a>function testArrayIndexOfOmitsDeleted() {
<a name="line35"></a>  var a = [0, 1, 2, 3];
<a name="line36"></a>  delete a[1];
<a name="line37"></a>  delete a[3];
<a name="line38"></a>  assertEquals(goog.array.indexOf(a, undefined), -1);
<a name="line39"></a>}
<a name="line40"></a>
<a name="line41"></a>function testArrayIndexOfString() {
<a name="line42"></a>  assertEquals(goog.array.indexOf(&#39;abcd&#39;, &#39;d&#39;), 3);
<a name="line43"></a>  assertEquals(goog.array.indexOf(&#39;abbb&#39;, &#39;b&#39;, 2), 2);
<a name="line44"></a>  assertEquals(goog.array.indexOf(&#39;abcd&#39;, &#39;e&#39;), -1);
<a name="line45"></a>  assertEquals(goog.array.indexOf(&#39;abcd&#39;, &#39;cd&#39;), -1);
<a name="line46"></a>  assertEquals(goog.array.indexOf(&#39;0123&#39;, 1), -1);
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>function testArrayLastIndexOf() {
<a name="line50"></a>  assertEquals(goog.array.lastIndexOf([0, 1, 2, 3], 1), 1);
<a name="line51"></a>  assertEquals(goog.array.lastIndexOf([0, 1, 1, 1], 1), 3);
<a name="line52"></a>  assertEquals(goog.array.lastIndexOf([0, 1, 1, 1], 1, 2), 2);
<a name="line53"></a>}
<a name="line54"></a>
<a name="line55"></a>function testArrayLastIndexOfOmitsDeleted() {
<a name="line56"></a>  var a = [0, 1, 2, 3];
<a name="line57"></a>  delete a[1];
<a name="line58"></a>  delete a[3];
<a name="line59"></a>  assertEquals(goog.array.lastIndexOf(a, undefined), -1);
<a name="line60"></a>}
<a name="line61"></a>
<a name="line62"></a>function testArrayLastIndexOfString() {
<a name="line63"></a>  assertEquals(goog.array.lastIndexOf(&#39;abcd&#39;, &#39;b&#39;), 1);
<a name="line64"></a>  assertEquals(goog.array.lastIndexOf(&#39;abbb&#39;, &#39;b&#39;), 3);
<a name="line65"></a>  assertEquals(goog.array.lastIndexOf(&#39;abbb&#39;, &#39;b&#39;, 2), 2);
<a name="line66"></a>  assertEquals(goog.array.lastIndexOf(&#39;abcd&#39;, &#39;cd&#39;), -1);
<a name="line67"></a>  assertEquals(goog.array.lastIndexOf(&#39;0123&#39;, 1), -1);
<a name="line68"></a>}
<a name="line69"></a>
<a name="line70"></a>function testArrayForEachBasic() {
<a name="line71"></a>  var s = &#39;&#39;;
<a name="line72"></a>  var a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line73"></a>  goog.array.forEach(a, function(val, index, a2) {
<a name="line74"></a>    assertEquals(a, a2);
<a name="line75"></a>    assertEquals(&#39;Index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line76"></a>    s += val + index;
<a name="line77"></a>  });
<a name="line78"></a>  assertEquals(&#39;a0b1c2d3&#39;, s);
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testArrayForEachWithEmptyArray() {
<a name="line82"></a>  var a = new Array(100);
<a name="line83"></a>  goog.array.forEach(a, function(val, index, a2) {
<a name="line84"></a>    fail(&#39;The function should not be called since no values were assigned.&#39;);
<a name="line85"></a>  });
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function testArrayForEachWithOnlySomeValuesAsigned() {
<a name="line89"></a>  var count = 0;
<a name="line90"></a>  var a = new Array(1000);
<a name="line91"></a>  a[100] = undefined;
<a name="line92"></a>  goog.array.forEach(a, function(val, index, a2) {
<a name="line93"></a>    assertEquals(100, index);
<a name="line94"></a>    count++;
<a name="line95"></a>  });
<a name="line96"></a>  assertEquals(&#39;Should only call function when a value of array was assigned.&#39;,
<a name="line97"></a>      1, count);
<a name="line98"></a>}
<a name="line99"></a>
<a name="line100"></a>function testArrayForEachWithArrayLikeObject() {
<a name="line101"></a>  var counter = goog.testing.recordFunction();
<a name="line102"></a>  var a = {
<a name="line103"></a>    &#39;length&#39;: 1,
<a name="line104"></a>    &#39;0&#39;: 0,
<a name="line105"></a>    &#39;100&#39;: 100,
<a name="line106"></a>    &#39;101&#39;: 102
<a name="line107"></a>  };
<a name="line108"></a>  goog.array.forEach(a, counter);
<a name="line109"></a>  assertEquals(&#39;Number of calls should not exceed the value of its length&#39;, 1,
<a name="line110"></a>      counter.getCallCount());
<a name="line111"></a>}
<a name="line112"></a>
<a name="line113"></a>function testArrayForEachOmitsDeleted() {
<a name="line114"></a>  var s = &#39;&#39;;
<a name="line115"></a>  var a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line116"></a>  delete a[1];
<a name="line117"></a>  delete a[3];
<a name="line118"></a>  goog.array.forEach(a, function(val, index, a2) {
<a name="line119"></a>    assertEquals(a, a2);
<a name="line120"></a>    assertEquals(&#39;number&#39;, typeof index);
<a name="line121"></a>    s += val + index;
<a name="line122"></a>  });
<a name="line123"></a>  assertEquals(&#39;a0c2&#39;, s);
<a name="line124"></a>}
<a name="line125"></a>
<a name="line126"></a>function testArrayForEachScope() {
<a name="line127"></a>  var scope = {};
<a name="line128"></a>  var a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line129"></a>  goog.array.forEach(a, function(val, index, a2) {
<a name="line130"></a>    assertEquals(a, a2);
<a name="line131"></a>    assertEquals(&#39;number&#39;, typeof index);
<a name="line132"></a>    assertEquals(this, scope);
<a name="line133"></a>  }, scope);
<a name="line134"></a>}
<a name="line135"></a>
<a name="line136"></a>function testArrayForEachRight() {
<a name="line137"></a>  var s = &#39;&#39;;
<a name="line138"></a>  var a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line139"></a>  goog.array.forEachRight(a, function(val, index, a2) {
<a name="line140"></a>    assertEquals(a, a2);
<a name="line141"></a>    assertEquals(&#39;number&#39;, typeof index);
<a name="line142"></a>    s += val + String(index);
<a name="line143"></a>  });
<a name="line144"></a>  assertEquals(&#39;d3c2b1a0&#39;, s);
<a name="line145"></a>}
<a name="line146"></a>
<a name="line147"></a>function testArrayForEachRightOmitsDeleted() {
<a name="line148"></a>  var s = &#39;&#39;;
<a name="line149"></a>  var a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line150"></a>  delete a[1];
<a name="line151"></a>  delete a[3];
<a name="line152"></a>  goog.array.forEachRight(a, function(val, index, a2) {
<a name="line153"></a>    assertEquals(a, a2);
<a name="line154"></a>    assertEquals(&#39;number&#39;, typeof index);
<a name="line155"></a>    assertEquals(&#39;string&#39;, typeof val);
<a name="line156"></a>    s += val + String(index);
<a name="line157"></a>  });
<a name="line158"></a>  assertEquals(&#39;c2a0&#39;, s);
<a name="line159"></a>}
<a name="line160"></a>
<a name="line161"></a>function testArrayFilter() {
<a name="line162"></a>  var a = [0, 1, 2, 3];
<a name="line163"></a>  a = goog.array.filter(a, function(val, index, a2) {
<a name="line164"></a>    assertEquals(a, a2);
<a name="line165"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line166"></a>    return val &gt; 1;
<a name="line167"></a>  });
<a name="line168"></a>  assertArrayEquals([2, 3], a);
<a name="line169"></a>}
<a name="line170"></a>
<a name="line171"></a>function testArrayFilterOmitsDeleted() {
<a name="line172"></a>  var a = [0, 1, 2, 3];
<a name="line173"></a>  delete a[1];
<a name="line174"></a>  delete a[3];
<a name="line175"></a>  a = goog.array.filter(a, function(val, index, a2) {
<a name="line176"></a>    assertEquals(a, a2);
<a name="line177"></a>    assertEquals(&#39;number&#39;, typeof val);
<a name="line178"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line179"></a>    return val &gt; 1;
<a name="line180"></a>  });
<a name="line181"></a>  assertArrayEquals([2], a);
<a name="line182"></a>}
<a name="line183"></a>
<a name="line184"></a>function testArrayFilterPreservesValues() {
<a name="line185"></a>  var a = [0, 1, 2, 3];
<a name="line186"></a>  a = goog.array.filter(a, function(val, index, a2) {
<a name="line187"></a>    assertEquals(a, a2);
<a name="line188"></a>    // sometimes functions might be evil and do something like this, but we
<a name="line189"></a>    // should still use the original values when returning the filtered array
<a name="line190"></a>    a2[index] = a2[index] - 1;
<a name="line191"></a>    return a2[index] &gt;= 1;
<a name="line192"></a>  });
<a name="line193"></a>  assertArrayEquals([2, 3], a);
<a name="line194"></a>}
<a name="line195"></a>
<a name="line196"></a>function testArrayMap() {
<a name="line197"></a>  var a = [0, 1, 2, 3];
<a name="line198"></a>  var result = goog.array.map(a, function(val, index, a2) {
<a name="line199"></a>    assertEquals(a, a2);
<a name="line200"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line201"></a>    return val * val;
<a name="line202"></a>  });
<a name="line203"></a>  assertArrayEquals([0, 1, 4, 9], result);
<a name="line204"></a>}
<a name="line205"></a>
<a name="line206"></a>function testArrayMapOmitsDeleted() {
<a name="line207"></a>  var a = [0, 1, 2, 3];
<a name="line208"></a>  delete a[1];
<a name="line209"></a>  delete a[3];
<a name="line210"></a>  var result = goog.array.map(a, function(val, index, a2) {
<a name="line211"></a>    assertEquals(a, a2);
<a name="line212"></a>    assertEquals(&#39;number&#39;, typeof val);
<a name="line213"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line214"></a>    return val * val;
<a name="line215"></a>  });
<a name="line216"></a>  var expected = [0, 1, 4, 9];
<a name="line217"></a>  delete expected[1];
<a name="line218"></a>  delete expected[3];
<a name="line219"></a>
<a name="line220"></a>  assertArrayEquals(expected, result);
<a name="line221"></a>  assertFalse(&#39;1&#39; in result);
<a name="line222"></a>  assertFalse(&#39;3&#39; in result);
<a name="line223"></a>}
<a name="line224"></a>
<a name="line225"></a>function testArrayReduce() {
<a name="line226"></a>  var a = [0, 1, 2, 3];
<a name="line227"></a>  assertEquals(6, goog.array.reduce(a, function(rval, val, i, arr) {
<a name="line228"></a>    assertEquals(&#39;number&#39;, typeof i);
<a name="line229"></a>    assertEquals(a, arr);
<a name="line230"></a>    return rval + val;
<a name="line231"></a>  }, 0));
<a name="line232"></a>
<a name="line233"></a>  var scope = {
<a name="line234"></a>    last: 0,
<a name="line235"></a>    testFn: function(r, v, i, arr) {
<a name="line236"></a>      assertEquals(&#39;number&#39;, typeof i);
<a name="line237"></a>      assertEquals(a, arr);
<a name="line238"></a>      var l = this.last;
<a name="line239"></a>      this.last = r + v;
<a name="line240"></a>      return this.last + l;
<a name="line241"></a>    }
<a name="line242"></a>  };
<a name="line243"></a>
<a name="line244"></a>  assertEquals(10, goog.array.reduce(a, scope.testFn, 0, scope));
<a name="line245"></a>}
<a name="line246"></a>
<a name="line247"></a>function testArrayReduceOmitDeleted() {
<a name="line248"></a>  var a = [0, 1, 2, 3];
<a name="line249"></a>  delete a[1];
<a name="line250"></a>  delete a[3];
<a name="line251"></a>  assertEquals(2, goog.array.reduce(a, function(rval, val, i, arr) {
<a name="line252"></a>    assertEquals(&#39;number&#39;, typeof i);
<a name="line253"></a>    assertEquals(a, arr);
<a name="line254"></a>    return rval + val;
<a name="line255"></a>  }, 0));
<a name="line256"></a>
<a name="line257"></a>  var scope = {
<a name="line258"></a>    last: 0,
<a name="line259"></a>    testFn: function(r, v, i, arr) {
<a name="line260"></a>      assertEquals(&#39;number&#39;, typeof i);
<a name="line261"></a>      assertEquals(a, arr);
<a name="line262"></a>      var l = this.last;
<a name="line263"></a>      this.last = r + v;
<a name="line264"></a>      return this.last + l;
<a name="line265"></a>    }
<a name="line266"></a>  };
<a name="line267"></a>
<a name="line268"></a>  assertEquals(2, goog.array.reduce(a, scope.testFn, 0, scope));
<a name="line269"></a>}
<a name="line270"></a>
<a name="line271"></a>function testArrayReduceRight() {
<a name="line272"></a>  var a = [0, 1, 2, 3, 4];
<a name="line273"></a>  assertEquals(&#39;43210&#39;, goog.array.reduceRight(a, function(rval, val, i, arr) {
<a name="line274"></a>    assertEquals(&#39;number&#39;, typeof i);
<a name="line275"></a>    assertEquals(a, arr);
<a name="line276"></a>    return rval + val;
<a name="line277"></a>  }, &#39;&#39;));
<a name="line278"></a>
<a name="line279"></a>  var scope = {
<a name="line280"></a>    last: &#39;&#39;,
<a name="line281"></a>    testFn: function(r, v, i, arr) {
<a name="line282"></a>      assertEquals(&#39;number&#39;, typeof i);
<a name="line283"></a>      assertEquals(a, arr);
<a name="line284"></a>      var l = this.last;
<a name="line285"></a>      this.last = v;
<a name="line286"></a>      return r + v + l;
<a name="line287"></a>    }
<a name="line288"></a>  };
<a name="line289"></a>
<a name="line290"></a>  a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];
<a name="line291"></a>  assertEquals(&#39;_cbcab&#39;, goog.array.reduceRight(a, scope.testFn, &#39;_&#39;, scope));
<a name="line292"></a>}
<a name="line293"></a>
<a name="line294"></a>function testArrayReduceRightOmitsDeleted() {
<a name="line295"></a>  var a = [0, 1, 2, 3, 4];
<a name="line296"></a>  delete a[1];
<a name="line297"></a>  delete a[4];
<a name="line298"></a>  assertEquals(&#39;320&#39;, goog.array.reduceRight(a, function(rval, val, i, arr) {
<a name="line299"></a>    assertEquals(&#39;number&#39;, typeof i);
<a name="line300"></a>    assertEquals(a, arr);
<a name="line301"></a>    return rval + val;
<a name="line302"></a>  }, &#39;&#39;));
<a name="line303"></a>
<a name="line304"></a>  scope = {
<a name="line305"></a>    last: &#39;&#39;,
<a name="line306"></a>    testFn: function(r, v, i, arr) {
<a name="line307"></a>      assertEquals(&#39;number&#39;, typeof i);
<a name="line308"></a>      assertEquals(a, arr);
<a name="line309"></a>      var l = this.last;
<a name="line310"></a>      this.last = v;
<a name="line311"></a>      return r + v + l;
<a name="line312"></a>    }
<a name="line313"></a>  };
<a name="line314"></a>
<a name="line315"></a>  a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line316"></a>  delete a[1];
<a name="line317"></a>  delete a[3];
<a name="line318"></a>  assertEquals(&#39;_cac&#39;, goog.array.reduceRight(a, scope.testFn, &#39;_&#39;, scope));
<a name="line319"></a>}
<a name="line320"></a>
<a name="line321"></a>function testArrayFind() {
<a name="line322"></a>  var a = [0, 1, 2, 3];
<a name="line323"></a>  var b = goog.array.find(a, function(val, index, a2) {
<a name="line324"></a>    assertEquals(a, a2);
<a name="line325"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line326"></a>    return val &gt; 1;
<a name="line327"></a>  });
<a name="line328"></a>  assertEquals(2, b);
<a name="line329"></a>
<a name="line330"></a>  b = goog.array.find(a, function(val, index, a2) {
<a name="line331"></a>    assertEquals(a, a2);
<a name="line332"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line333"></a>    return val &gt; 100;
<a name="line334"></a>  });
<a name="line335"></a>  assertNull(b);
<a name="line336"></a>
<a name="line337"></a>  a = &#39;abCD&#39;;
<a name="line338"></a>  b = goog.array.find(a, function(val, index, a2) {
<a name="line339"></a>    assertEquals(a, a2);
<a name="line340"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line341"></a>    return val &gt;= &#39;A&#39; &amp;&amp; val &lt;= &#39;Z&#39;;
<a name="line342"></a>  });
<a name="line343"></a>  assertEquals(&#39;C&#39;, b);
<a name="line344"></a>
<a name="line345"></a>  a = &#39;abcd&#39;;
<a name="line346"></a>  b = goog.array.find(a, function(val, index, a2) {
<a name="line347"></a>    assertEquals(a, a2);
<a name="line348"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line349"></a>    return val &gt;= &#39;A&#39; &amp;&amp; val &lt;= &#39;Z&#39;;
<a name="line350"></a>  });
<a name="line351"></a>  assertNull(b);
<a name="line352"></a>}
<a name="line353"></a>
<a name="line354"></a>function testArrayFindOmitsDeleted() {
<a name="line355"></a>  var a = [0, 1, 2, 3];
<a name="line356"></a>  delete a[1];
<a name="line357"></a>  delete a[3];
<a name="line358"></a>  var b = goog.array.find(a, function(val, index, a2) {
<a name="line359"></a>    assertEquals(a, a2);
<a name="line360"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line361"></a>    return val &gt; 1;
<a name="line362"></a>  });
<a name="line363"></a>
<a name="line364"></a>  assertEquals(2, b);
<a name="line365"></a>  b = goog.array.find(a, function(val, index, a2) {
<a name="line366"></a>    assertEquals(a, a2);
<a name="line367"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line368"></a>    return val &gt; 100;
<a name="line369"></a>  });
<a name="line370"></a>  assertNull(b);
<a name="line371"></a>}
<a name="line372"></a>
<a name="line373"></a>function testArrayFindIndex() {
<a name="line374"></a>  var a = [0, 1, 2, 3];
<a name="line375"></a>  var b = goog.array.findIndex(a, function(val, index, a2) {
<a name="line376"></a>    assertEquals(a, a2);
<a name="line377"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line378"></a>    return val &gt; 1;
<a name="line379"></a>  });
<a name="line380"></a>  assertEquals(2, b);
<a name="line381"></a>
<a name="line382"></a>  b = goog.array.findIndex(a, function(val, index, a2) {
<a name="line383"></a>    assertEquals(a, a2);
<a name="line384"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line385"></a>    return val &gt; 100;
<a name="line386"></a>  });
<a name="line387"></a>  assertEquals(-1, b);
<a name="line388"></a>
<a name="line389"></a>  a = &#39;abCD&#39;;
<a name="line390"></a>  b = goog.array.findIndex(a, function(val, index, a2) {
<a name="line391"></a>    assertEquals(a, a2);
<a name="line392"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line393"></a>    return val &gt;= &#39;A&#39; &amp;&amp; val &lt;= &#39;Z&#39;;
<a name="line394"></a>  });
<a name="line395"></a>  assertEquals(2, b);
<a name="line396"></a>
<a name="line397"></a>  a = &#39;abcd&#39;;
<a name="line398"></a>  b = goog.array.findIndex(a, function(val, index, a2) {
<a name="line399"></a>    assertEquals(a, a2);
<a name="line400"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line401"></a>    return val &gt;= &#39;A&#39; &amp;&amp; val &lt;= &#39;Z&#39;;
<a name="line402"></a>  });
<a name="line403"></a>  assertEquals(-1, b);
<a name="line404"></a>}
<a name="line405"></a>
<a name="line406"></a>function testArrayFindIndexOmitsDeleted() {
<a name="line407"></a>  var a = [0, 1, 2, 3];
<a name="line408"></a>  delete a[1];
<a name="line409"></a>  delete a[3];
<a name="line410"></a>  var b = goog.array.findIndex(a, function(val, index, a2) {
<a name="line411"></a>    assertEquals(a, a2);
<a name="line412"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line413"></a>    return val &gt; 1;
<a name="line414"></a>  });
<a name="line415"></a>  assertEquals(2, b);
<a name="line416"></a>
<a name="line417"></a>  b = goog.array.findIndex(a, function(val, index, a2) {
<a name="line418"></a>    assertEquals(a, a2);
<a name="line419"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line420"></a>    return val &gt; 100;
<a name="line421"></a>  });
<a name="line422"></a>  assertEquals(-1, b);
<a name="line423"></a>}
<a name="line424"></a>
<a name="line425"></a>function testArrayFindRight() {
<a name="line426"></a>  var a = [0, 1, 2, 3];
<a name="line427"></a>  var b = goog.array.findRight(a, function(val, index, a2) {
<a name="line428"></a>    assertEquals(a, a2);
<a name="line429"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line430"></a>    return val &lt; 3;
<a name="line431"></a>  });
<a name="line432"></a>  assertEquals(2, b);
<a name="line433"></a>  b = goog.array.findRight(a, function(val, index, a2) {
<a name="line434"></a>    assertEquals(a, a2);
<a name="line435"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line436"></a>    return val &gt; 100;
<a name="line437"></a>  });
<a name="line438"></a>  assertNull(b);
<a name="line439"></a>}
<a name="line440"></a>
<a name="line441"></a>function testArrayFindRightOmitsDeleted() {
<a name="line442"></a>  var a = [0, 1, 2, 3];
<a name="line443"></a>  delete a[1];
<a name="line444"></a>  delete a[3];
<a name="line445"></a>  var b = goog.array.findRight(a, function(val, index, a2) {
<a name="line446"></a>    assertEquals(a, a2);
<a name="line447"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line448"></a>    return val &lt; 3;
<a name="line449"></a>  });
<a name="line450"></a>  assertEquals(2, b);
<a name="line451"></a>  b = goog.array.findRight(a, function(val, index, a2) {
<a name="line452"></a>    assertEquals(a, a2);
<a name="line453"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line454"></a>    return val &gt; 100;
<a name="line455"></a>  });
<a name="line456"></a>  assertNull(b);
<a name="line457"></a>}
<a name="line458"></a>
<a name="line459"></a>function testArrayFindIndexRight() {
<a name="line460"></a>  var a = [0, 1, 2, 3];
<a name="line461"></a>  var b = goog.array.findIndexRight(a, function(val, index, a2) {
<a name="line462"></a>    assertEquals(a, a2);
<a name="line463"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line464"></a>    return val &lt; 3;
<a name="line465"></a>  });
<a name="line466"></a>  assertEquals(2, b);
<a name="line467"></a>
<a name="line468"></a>  b = goog.array.findIndexRight(a, function(val, index, a2) {
<a name="line469"></a>    assertEquals(a, a2);
<a name="line470"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line471"></a>    return val &gt; 100;
<a name="line472"></a>  });
<a name="line473"></a>  assertEquals(-1, b);
<a name="line474"></a>
<a name="line475"></a>  a = &#39;abCD&#39;;
<a name="line476"></a>  b = goog.array.findIndexRight(a, function(val, index, a2) {
<a name="line477"></a>    assertEquals(a, a2);
<a name="line478"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line479"></a>    return val &gt;= &#39;a&#39; &amp;&amp; val &lt;= &#39;z&#39;;
<a name="line480"></a>  });
<a name="line481"></a>  assertEquals(1, b);
<a name="line482"></a>
<a name="line483"></a>  a = &#39;abcd&#39;;
<a name="line484"></a>  b = goog.array.findIndexRight(a, function(val, index, a2) {
<a name="line485"></a>    assertEquals(a, a2);
<a name="line486"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line487"></a>    return val &gt;= &#39;A&#39; &amp;&amp; val &lt;= &#39;Z&#39;;
<a name="line488"></a>  });
<a name="line489"></a>  assertEquals(-1, b);
<a name="line490"></a>}
<a name="line491"></a>
<a name="line492"></a>function testArrayFindIndexRightOmitsDeleted() {
<a name="line493"></a>  var a = [0, 1, 2, 3];
<a name="line494"></a>  delete a[1];
<a name="line495"></a>  delete a[3];
<a name="line496"></a>  var b = goog.array.findIndexRight(a, function(val, index, a2) {
<a name="line497"></a>    assertEquals(a, a2);
<a name="line498"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line499"></a>    return val &lt; 3;
<a name="line500"></a>  });
<a name="line501"></a>  assertEquals(2, b);
<a name="line502"></a>  b = goog.array.findIndexRight(a, function(val, index, a2) {
<a name="line503"></a>    assertEquals(a, a2);
<a name="line504"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line505"></a>    return val &gt; 100;
<a name="line506"></a>  });
<a name="line507"></a>  assertEquals(-1, b);
<a name="line508"></a>}
<a name="line509"></a>
<a name="line510"></a>function testArraySome() {
<a name="line511"></a>  var a = [0, 1, 2, 3];
<a name="line512"></a>  var b = goog.array.some(a, function(val, index, a2) {
<a name="line513"></a>    assertEquals(a, a2);
<a name="line514"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line515"></a>    return val &gt; 1;
<a name="line516"></a>  });
<a name="line517"></a>  assertTrue(b);
<a name="line518"></a>  b = goog.array.some(a, function(val, index, a2) {
<a name="line519"></a>    assertEquals(a, a2);
<a name="line520"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line521"></a>    return val &gt; 100;
<a name="line522"></a>  });
<a name="line523"></a>  assertFalse(b);
<a name="line524"></a>}
<a name="line525"></a>
<a name="line526"></a>function testArraySomeOmitsDeleted() {
<a name="line527"></a>  var a = [0, 1, 2, 3];
<a name="line528"></a>  delete a[1];
<a name="line529"></a>  delete a[3];
<a name="line530"></a>  var b = goog.array.some(a, function(val, index, a2) {
<a name="line531"></a>    assertEquals(a, a2);
<a name="line532"></a>    assertEquals(&#39;number&#39;, typeof val);
<a name="line533"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line534"></a>    return val &gt; 1;
<a name="line535"></a>  });
<a name="line536"></a>  assertTrue(b);
<a name="line537"></a>  b = goog.array.some(a, function(val, index, a2) {
<a name="line538"></a>    assertEquals(a, a2);
<a name="line539"></a>    assertEquals(&#39;number&#39;, typeof val);
<a name="line540"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line541"></a>    return val &gt; 100;
<a name="line542"></a>  });
<a name="line543"></a>  assertFalse(b);
<a name="line544"></a>}
<a name="line545"></a>
<a name="line546"></a>function testArrayEvery() {
<a name="line547"></a>  var a = [0, 1, 2, 3];
<a name="line548"></a>  var b = goog.array.every(a, function(val, index, a2) {
<a name="line549"></a>    assertEquals(a, a2);
<a name="line550"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line551"></a>    return val &gt;= 0;
<a name="line552"></a>  });
<a name="line553"></a>  assertTrue(b);
<a name="line554"></a>  b = goog.array.every(a, function(val, index, a2) {
<a name="line555"></a>    assertEquals(a, a2);
<a name="line556"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line557"></a>    return val &gt; 1;
<a name="line558"></a>  });
<a name="line559"></a>  assertFalse(b);
<a name="line560"></a>}
<a name="line561"></a>
<a name="line562"></a>function testArrayEveryOmitsDeleted() {
<a name="line563"></a>  var a = [0, 1, 2, 3];
<a name="line564"></a>  delete a[1];
<a name="line565"></a>  delete a[3];
<a name="line566"></a>  var b = goog.array.every(a, function(val, index, a2) {
<a name="line567"></a>    assertEquals(a, a2);
<a name="line568"></a>    assertEquals(&#39;number&#39;, typeof val);
<a name="line569"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line570"></a>    return val &gt;= 0;
<a name="line571"></a>  });
<a name="line572"></a>  assertTrue(b);
<a name="line573"></a>  b = goog.array.every(a, function(val, index, a2) {
<a name="line574"></a>    assertEquals(a, a2);
<a name="line575"></a>    assertEquals(&#39;number&#39;, typeof val);
<a name="line576"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line577"></a>    return val &gt; 1;
<a name="line578"></a>  });
<a name="line579"></a>  assertFalse(b);
<a name="line580"></a>}
<a name="line581"></a>
<a name="line582"></a>function testArrayCount() {
<a name="line583"></a>  var a = [0, 1, 2, 3, 4];
<a name="line584"></a>  var context = {};
<a name="line585"></a>  assertEquals(3, goog.array.count(a, function(element, index, array) {
<a name="line586"></a>    assertTrue(goog.isNumber(index));
<a name="line587"></a>    assertEquals(a, array);
<a name="line588"></a>    assertEquals(context, this);
<a name="line589"></a>    return element % 2 == 0;
<a name="line590"></a>  }, context));
<a name="line591"></a>
<a name="line592"></a>  delete a[2];
<a name="line593"></a>  assertEquals(&#39;deleted element is ignored&#39;, 4,
<a name="line594"></a>      goog.array.count(a, function() {
<a name="line595"></a>        return true;
<a name="line596"></a>      }));
<a name="line597"></a>}
<a name="line598"></a>
<a name="line599"></a>function testArrayContains() {
<a name="line600"></a>  var a = [0, 1, 2, 3];
<a name="line601"></a>  assertTrue(&#39;contain, Should contain 3&#39;, goog.array.contains(a, 3));
<a name="line602"></a>  assertFalse(&#39;contain, Should not contain 4&#39;, goog.array.contains(a, 4));
<a name="line603"></a>
<a name="line604"></a>  var s = &#39;abcd&#39;;
<a name="line605"></a>  assertTrue(&#39;contain, Should contain d&#39;, goog.array.contains(s, &#39;d&#39;));
<a name="line606"></a>  assertFalse(&#39;contain, Should not contain e&#39;, goog.array.contains(s, &#39;e&#39;));
<a name="line607"></a>}
<a name="line608"></a>
<a name="line609"></a>function testArrayContainsOmitsDeleted() {
<a name="line610"></a>  var a = [0, 1, 2, 3];
<a name="line611"></a>  delete a[1];
<a name="line612"></a>  delete a[3];
<a name="line613"></a>  assertFalse(&#39;should not contain undefined&#39;,
<a name="line614"></a>      goog.array.contains(a, undefined));
<a name="line615"></a>}
<a name="line616"></a>
<a name="line617"></a>function testArrayInsert() {
<a name="line618"></a>  var a = [0, 1, 2, 3];
<a name="line619"></a>
<a name="line620"></a>  goog.array.insert(a, 4);
<a name="line621"></a>  assertEquals(&#39;insert, Should append 4&#39;, a[4], 4);
<a name="line622"></a>  goog.array.insert(a, 3);
<a name="line623"></a>  assertEquals(&#39;insert, Should not append 3&#39;, a.length, 5);
<a name="line624"></a>  assertNotEquals(&#39;insert, Should not append 3&#39;, a[a.length - 1], 3);
<a name="line625"></a>}
<a name="line626"></a>
<a name="line627"></a>function testArrayInsertAt() {
<a name="line628"></a>  var a = [0, 1, 2, 3];
<a name="line629"></a>
<a name="line630"></a>  goog.array.insertAt(a, 4, 2);
<a name="line631"></a>  assertArrayEquals(&#39;insertAt, insert in middle&#39;, [0, 1, 4, 2, 3], a);
<a name="line632"></a>  goog.array.insertAt(a, 5, 10);
<a name="line633"></a>  assertArrayEquals(&#39;insertAt, too large value should append&#39;,
<a name="line634"></a>                    [0, 1, 4, 2, 3, 5], a);
<a name="line635"></a>  goog.array.insertAt(a, 6);
<a name="line636"></a>  assertArrayEquals(&#39;insertAt, null/undefined value should insert at 0&#39;,
<a name="line637"></a>                    [6, 0, 1, 4, 2, 3, 5], a);
<a name="line638"></a>  goog.array.insertAt(a, 7, -2);
<a name="line639"></a>  assertArrayEquals(&#39;insertAt, negative values start from end&#39;,
<a name="line640"></a>                    [6, 0, 1, 4, 2, 7, 3, 5], a);
<a name="line641"></a>}
<a name="line642"></a>
<a name="line643"></a>function testArrayInsertArrayAt() {
<a name="line644"></a>  var a = [2, 5];
<a name="line645"></a>  goog.array.insertArrayAt(a, [3, 4], 1);
<a name="line646"></a>  assertArrayEquals(&#39;insertArrayAt, insert in middle&#39;, [2, 3, 4, 5], a);
<a name="line647"></a>  goog.array.insertArrayAt(a, [0, 1], 0);
<a name="line648"></a>  assertArrayEquals(&#39;insertArrayAt, insert at beginning&#39;,
<a name="line649"></a>                    [0, 1, 2, 3, 4, 5], a);
<a name="line650"></a>  goog.array.insertArrayAt(a, [6, 7], 6);
<a name="line651"></a>  assertArrayEquals(&#39;insertArrayAt, insert at end&#39;,
<a name="line652"></a>                    [0, 1, 2, 3, 4, 5, 6, 7], a);
<a name="line653"></a>  goog.array.insertArrayAt(a, [&#39;x&#39;], 4);
<a name="line654"></a>  assertArrayEquals(&#39;insertArrayAt, insert one element&#39;,
<a name="line655"></a>                    [0, 1, 2, 3, &#39;x&#39;, 4, 5, 6, 7], a);
<a name="line656"></a>  goog.array.insertArrayAt(a, [], 4);
<a name="line657"></a>  assertArrayEquals(&#39;insertArrayAt, insert 0 elements&#39;,
<a name="line658"></a>                    [0, 1, 2, 3, &#39;x&#39;, 4, 5, 6, 7], a);
<a name="line659"></a>  goog.array.insertArrayAt(a, [&#39;y&#39;, &#39;z&#39;]);
<a name="line660"></a>  assertArrayEquals(&#39;insertArrayAt, undefined value should insert at 0&#39;,
<a name="line661"></a>      [&#39;y&#39;, &#39;z&#39;, 0, 1, 2, 3, &#39;x&#39;, 4, 5, 6, 7], a);
<a name="line662"></a>  goog.array.insertArrayAt(a, [&#39;a&#39;], null);
<a name="line663"></a>  assertArrayEquals(&#39;insertArrayAt, null value should insert at 0&#39;,
<a name="line664"></a>      [&#39;a&#39;, &#39;y&#39;, &#39;z&#39;, 0, 1, 2, 3, &#39;x&#39;, 4, 5, 6, 7], a);
<a name="line665"></a>  goog.array.insertArrayAt(a, [&#39;b&#39;], 100);
<a name="line666"></a>  assertArrayEquals(&#39;insertArrayAt, too large value should append&#39;,
<a name="line667"></a>      [&#39;a&#39;, &#39;y&#39;, &#39;z&#39;, 0, 1, 2, 3, &#39;x&#39;, 4, 5, 6, 7, &#39;b&#39;], a);
<a name="line668"></a>  goog.array.insertArrayAt(a, [&#39;c&#39;, &#39;d&#39;], -2);
<a name="line669"></a>  assertArrayEquals(&#39;insertArrayAt, negative values start from end&#39;,
<a name="line670"></a>      [&#39;a&#39;, &#39;y&#39;, &#39;z&#39;, 0, 1, 2, 3, &#39;x&#39;, 4, 5, 6, &#39;c&#39;, &#39;d&#39;, 7, &#39;b&#39;], a);
<a name="line671"></a>}
<a name="line672"></a>
<a name="line673"></a>function testArrayInsertBefore() {
<a name="line674"></a>  var a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line675"></a>  goog.array.insertBefore(a, &#39;e&#39;, &#39;b&#39;);
<a name="line676"></a>  assertArrayEquals(&#39;insertBefore, with existing element&#39;,
<a name="line677"></a>                    [&#39;a&#39;, &#39;e&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], a);
<a name="line678"></a>  goog.array.insertBefore(a, &#39;f&#39;, &#39;x&#39;);
<a name="line679"></a>  assertArrayEquals(&#39;insertBefore, with non existing element&#39;,
<a name="line680"></a>                    [&#39;a&#39;, &#39;e&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;f&#39;], a);
<a name="line681"></a>}
<a name="line682"></a>
<a name="line683"></a>function testArrayRemove() {
<a name="line684"></a>  var a = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;];
<a name="line685"></a>  goog.array.remove(a, &#39;c&#39;);
<a name="line686"></a>  assertArrayEquals(&#39;remove, remove existing element&#39;, [&#39;a&#39;, &#39;b&#39;, &#39;d&#39;], a);
<a name="line687"></a>  goog.array.remove(a, &#39;x&#39;);
<a name="line688"></a>  assertArrayEquals(&#39;remove, remove non existing element&#39;, [&#39;a&#39;, &#39;b&#39;, &#39;d&#39;], a);
<a name="line689"></a>}
<a name="line690"></a>
<a name="line691"></a>function testArrayRemoveAt() {
<a name="line692"></a>  var a = [0, 1, 2, 3];
<a name="line693"></a>  goog.array.removeAt(a, 2);
<a name="line694"></a>  assertArrayEquals(&#39;removeAt, remove existing index&#39;, [0, 1, 3], a);
<a name="line695"></a>  a = [0, 1, 2, 3];
<a name="line696"></a>  goog.array.removeAt(a, 10);
<a name="line697"></a>  assertArrayEquals(&#39;removeAt, remove non existing index&#39;, [0, 1, 2, 3], a);
<a name="line698"></a>  a = [0, 1, 2, 3];
<a name="line699"></a>  goog.array.removeAt(a, -2);
<a name="line700"></a>  assertArrayEquals(&#39;removeAt, remove with negative index&#39;, [0, 1, 3], a);
<a name="line701"></a>}
<a name="line702"></a>
<a name="line703"></a>function testArrayRemoveIf() {
<a name="line704"></a>  var a = [0, 1, 2, 3];
<a name="line705"></a>  var b = goog.array.removeIf(a, function(val, index, a2) {
<a name="line706"></a>    assertEquals(a, a2);
<a name="line707"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line708"></a>    return val &gt; 1;
<a name="line709"></a>  });
<a name="line710"></a>  assertArrayEquals(&#39;removeIf, remove existing element&#39;, [0, 1, 3], a);
<a name="line711"></a>
<a name="line712"></a>  a = [0, 1, 2, 3];
<a name="line713"></a>  var b = goog.array.removeIf(a, function(val, index, a2) {
<a name="line714"></a>    assertEquals(a, a2);
<a name="line715"></a>    assertEquals(&#39;index is not a number&#39;, &#39;number&#39;, typeof index);
<a name="line716"></a>    return val &gt; 100;
<a name="line717"></a>  });
<a name="line718"></a>  assertArrayEquals(&#39;removeIf, remove non-existing element&#39;, [0, 1, 2, 3], a);
<a name="line719"></a>}
<a name="line720"></a>
<a name="line721"></a>function testArrayClone() {
<a name="line722"></a>  var a = [0, 1, 2, 3];
<a name="line723"></a>  var a2 = goog.array.clone(a);
<a name="line724"></a>  assertArrayEquals(&#39;clone, should be equal&#39;, a, a2);
<a name="line725"></a>
<a name="line726"></a>  var b = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
<a name="line727"></a>  var b2 = goog.array.clone(b);
<a name="line728"></a>  for (var i = 0; i &lt; b.length; i++) {
<a name="line729"></a>    assertEquals(&#39;clone, should be equal&#39;, b[i], b2[i]);
<a name="line730"></a>  }
<a name="line731"></a>}
<a name="line732"></a>
<a name="line733"></a>function testToArray() {
<a name="line734"></a>  var a = [0, 1, 2, 3];
<a name="line735"></a>  var a2 = goog.array.toArray(a);
<a name="line736"></a>  assertArrayEquals(&#39;toArray, should be equal&#39;, a, a2);
<a name="line737"></a>
<a name="line738"></a>  var b = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
<a name="line739"></a>  var b2 = goog.array.toArray(b);
<a name="line740"></a>  for (var i = 0; i &lt; b.length; i++) {
<a name="line741"></a>    assertEquals(&#39;toArray, should be equal&#39;, b[i], b2[i]);
<a name="line742"></a>  }
<a name="line743"></a>}
<a name="line744"></a>
<a name="line745"></a>function testToArrayOnNonArrayLike() {
<a name="line746"></a>  var nonArrayLike = {};
<a name="line747"></a>  assertArrayEquals(&#39;toArray on non ArrayLike should return an empty array&#39;,
<a name="line748"></a>                    [], goog.array.toArray(nonArrayLike));
<a name="line749"></a>
<a name="line750"></a>  var nonArrayLike2 = {length: &#39;hello world&#39;};
<a name="line751"></a>  assertArrayEquals(&#39;toArray on non ArrayLike should return an empty array&#39;,
<a name="line752"></a>                    [], goog.array.toArray(nonArrayLike2));
<a name="line753"></a>}
<a name="line754"></a>
<a name="line755"></a>function testExtend() {
<a name="line756"></a>  var a = [0, 1];
<a name="line757"></a>  goog.array.extend(a, [2, 3]);
<a name="line758"></a>  var a2 = [0, 1, 2, 3];
<a name="line759"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, a, a2);
<a name="line760"></a>
<a name="line761"></a>  var b = [0, 1];
<a name="line762"></a>  goog.array.extend(b, 2);
<a name="line763"></a>  var b2 = [0, 1, 2];
<a name="line764"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, b, b2);
<a name="line765"></a>
<a name="line766"></a>  a = [0, 1];
<a name="line767"></a>  goog.array.extend(a, [2, 3], [4, 5]);
<a name="line768"></a>  a2 = [0, 1, 2, 3, 4, 5];
<a name="line769"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, a, a2);
<a name="line770"></a>
<a name="line771"></a>  b = [0, 1];
<a name="line772"></a>  goog.array.extend(b, 2, 3);
<a name="line773"></a>  b2 = [0, 1, 2, 3];
<a name="line774"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, b, b2);
<a name="line775"></a>
<a name="line776"></a>  var c = [0, 1];
<a name="line777"></a>  goog.array.extend(c, 2, [3, 4], 5, [6]);
<a name="line778"></a>  var c2 = [0, 1, 2, 3, 4, 5, 6];
<a name="line779"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, c, c2);
<a name="line780"></a>
<a name="line781"></a>  var d = [0, 1];
<a name="line782"></a>  var arrayLikeObject = {0: 2, 1: 3, length: 2};
<a name="line783"></a>  goog.array.extend(d, arrayLikeObject);
<a name="line784"></a>  var d2 = [0, 1, 2, 3];
<a name="line785"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, d, d2);
<a name="line786"></a>
<a name="line787"></a>  var e = [0, 1];
<a name="line788"></a>  var emptyArrayLikeObject = {length: 0};
<a name="line789"></a>  goog.array.extend(e, emptyArrayLikeObject);
<a name="line790"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, e, e);
<a name="line791"></a>
<a name="line792"></a>  var f = [0, 1];
<a name="line793"></a>  var length3ArrayLikeObject = {0: 2, 1: 4, 2: 8, length: 3};
<a name="line794"></a>  goog.array.extend(f, length3ArrayLikeObject, length3ArrayLikeObject);
<a name="line795"></a>  var f2 = [0, 1, 2, 4, 8, 2, 4, 8];
<a name="line796"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, f2, f);
<a name="line797"></a>}
<a name="line798"></a>
<a name="line799"></a>function testExtendWithArguments() {
<a name="line800"></a>  function f() {
<a name="line801"></a>    return arguments;
<a name="line802"></a>  }
<a name="line803"></a>  var a = [0];
<a name="line804"></a>  var a2 = [0, 1, 2, 3, 4, 5];
<a name="line805"></a>  goog.array.extend(a, f(1, 2, 3), f(4, 5));
<a name="line806"></a>  assertArrayEquals(&#39;extend, should be equal&#39;, a, a2);
<a name="line807"></a>}
<a name="line808"></a>
<a name="line809"></a>function testExtendWithQuerySelector() {
<a name="line810"></a>  var a = [0];
<a name="line811"></a>  var d = goog.dom.getElementsByTagNameAndClass(&#39;div&#39;, &#39;foo&#39;);
<a name="line812"></a>  goog.array.extend(a, d);
<a name="line813"></a>  assertEquals(2, a.length);
<a name="line814"></a>}
<a name="line815"></a>
<a name="line816"></a>function testArraySplice() {
<a name="line817"></a>  var a = [0, 1, 2, 3];
<a name="line818"></a>  goog.array.splice(a, 1, 0, 4);
<a name="line819"></a>  assertArrayEquals([0, 4, 1, 2, 3], a);
<a name="line820"></a>  goog.array.splice(a, 1, 1, 5);
<a name="line821"></a>  assertArrayEquals([0, 5, 1, 2, 3], a);
<a name="line822"></a>  goog.array.splice(a, 1, 1);
<a name="line823"></a>  assertArrayEquals([0, 1, 2, 3], a);
<a name="line824"></a>  // var args
<a name="line825"></a>  goog.array.splice(a, 1, 1, 4, 5, 6);
<a name="line826"></a>  assertArrayEquals([0, 4, 5, 6, 2, 3], a);
<a name="line827"></a>}
<a name="line828"></a>
<a name="line829"></a>function testArraySlice() {
<a name="line830"></a>  var a = [0, 1, 2, 3];
<a name="line831"></a>  a = goog.array.slice(a, 1, 3);
<a name="line832"></a>  assertArrayEquals([1, 2], a);
<a name="line833"></a>  a = [0, 1, 2, 3];
<a name="line834"></a>  a = goog.array.slice(a, 1, 6);
<a name="line835"></a>  assertArrayEquals(&#39;slice, with too large end&#39;, [1, 2, 3], a);
<a name="line836"></a>  a = [0, 1, 2, 3];
<a name="line837"></a>  a = goog.array.slice(a, 1, -1);
<a name="line838"></a>  assertArrayEquals(&#39;slice, with negative end&#39;, [1, 2], a);
<a name="line839"></a>  a = [0, 1, 2, 3];
<a name="line840"></a>  a = goog.array.slice(a, -2, 3);
<a name="line841"></a>  assertArrayEquals(&#39;slice, with negative start&#39;, [2], a);
<a name="line842"></a>}
<a name="line843"></a>
<a name="line844"></a>function assertRemovedDuplicates(expected, original) {
<a name="line845"></a>  var tempArr = goog.array.clone(original);
<a name="line846"></a>  goog.array.removeDuplicates(tempArr);
<a name="line847"></a>  assertArrayEquals(expected, tempArr);
<a name="line848"></a>}
<a name="line849"></a>
<a name="line850"></a>function testRemoveDuplicates() {
<a name="line851"></a>  assertRemovedDuplicates([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]);
<a name="line852"></a>  assertRemovedDuplicates([9, 4, 2, 1, 3, 6, 0, -9],
<a name="line853"></a>                          [9, 4, 2, 4, 4, 2, 9, 1, 3, 6, 0, -9]);
<a name="line854"></a>  assertRemovedDuplicates([&#39;four&#39;, &#39;one&#39;, &#39;two&#39;, &#39;three&#39;, &#39;THREE&#39;],
<a name="line855"></a>      [&#39;four&#39;, &#39;one&#39;, &#39;two&#39;, &#39;one&#39;, &#39;three&#39;, &#39;THREE&#39;, &#39;four&#39;, &#39;two&#39;]);
<a name="line856"></a>  assertRemovedDuplicates([], []);
<a name="line857"></a>  assertRemovedDuplicates(
<a name="line858"></a>      [&#39;abc&#39;, &#39;hasOwnProperty&#39;, &#39;toString&#39;],
<a name="line859"></a>      [&#39;abc&#39;, &#39;hasOwnProperty&#39;, &#39;toString&#39;, &#39;abc&#39;]);
<a name="line860"></a>
<a name="line861"></a>  var o1 = {}, o2 = {}, o3 = {}, o4 = {};
<a name="line862"></a>  assertRemovedDuplicates([o1, o2, o3, o4], [o1, o1, o2, o3, o2, o4]);
<a name="line863"></a>
<a name="line864"></a>  // Mixed object types.
<a name="line865"></a>  assertRemovedDuplicates([1, &#39;1&#39;, 2, &#39;2&#39;], [1, &#39;1&#39;, 2, &#39;2&#39;]);
<a name="line866"></a>  assertRemovedDuplicates([true, &#39;true&#39;, false, &#39;false&#39;],
<a name="line867"></a>                          [true, &#39;true&#39;, false, &#39;false&#39;]);
<a name="line868"></a>  assertRemovedDuplicates([&#39;foo&#39;], [String(&#39;foo&#39;), &#39;foo&#39;]);
<a name="line869"></a>  assertRemovedDuplicates([12], [Number(12), 12]);
<a name="line870"></a>
<a name="line871"></a>  var obj = {};
<a name="line872"></a>  var uid = goog.getUid(obj);
<a name="line873"></a>  assertRemovedDuplicates([obj, uid], [obj, uid]);
<a name="line874"></a>}
<a name="line875"></a>
<a name="line876"></a>function testRemoveDuplicates_customHashFn() {
<a name="line877"></a>  var object1 = {key: &#39;foo&#39;};
<a name="line878"></a>  var object2 = {key: &#39;bar&#39;};
<a name="line879"></a>  var dupeObject = {key: &#39;foo&#39;};
<a name="line880"></a>  var array = [object1, object2, dupeObject, &#39;bar&#39;];
<a name="line881"></a>  var hashFn = function(object) {
<a name="line882"></a>    return goog.isObject(object) ? object.key :
<a name="line883"></a>        (typeof object).charAt(0) + object;
<a name="line884"></a>  };
<a name="line885"></a>  goog.array.removeDuplicates(array, /* opt_rv */ undefined, hashFn);
<a name="line886"></a>  assertArrayEquals([object1, object2, &#39;bar&#39;], array);
<a name="line887"></a>}
<a name="line888"></a>
<a name="line889"></a>function testBinaryInsertRemove() {
<a name="line890"></a>  var makeChecker = function(array, fn, opt_compareFn) {
<a name="line891"></a>    return function(value, expectResult, expectArray) {
<a name="line892"></a>      var result = fn(array, value, opt_compareFn);
<a name="line893"></a>      assertEquals(expectResult, result);
<a name="line894"></a>      assertArrayEquals(expectArray, array);
<a name="line895"></a>    }
<a name="line896"></a>  };
<a name="line897"></a>
<a name="line898"></a>  var a = [];
<a name="line899"></a>  var check = makeChecker(a, goog.array.binaryInsert);
<a name="line900"></a>  check(3, true, [3]);
<a name="line901"></a>  check(3, false, [3]);
<a name="line902"></a>  check(1, true, [1, 3]);
<a name="line903"></a>  check(5, true, [1, 3, 5]);
<a name="line904"></a>  check(2, true, [1, 2, 3, 5]);
<a name="line905"></a>  check(2, false, [1, 2, 3, 5]);
<a name="line906"></a>
<a name="line907"></a>  check = makeChecker(a, goog.array.binaryRemove);
<a name="line908"></a>  check(0, false, [1, 2, 3, 5]);
<a name="line909"></a>  check(3, true, [1, 2, 5]);
<a name="line910"></a>  check(1, true, [2, 5]);
<a name="line911"></a>  check(5, true, [2]);
<a name="line912"></a>  check(2, true, []);
<a name="line913"></a>  check(2, false, []);
<a name="line914"></a>
<a name="line915"></a>  // test with custom comparison function, which reverse orders numbers
<a name="line916"></a>  var revNumCompare = function(a, b) { return b - a; };
<a name="line917"></a>
<a name="line918"></a>  check = makeChecker(a, goog.array.binaryInsert, revNumCompare);
<a name="line919"></a>  check(3, true, [3]);
<a name="line920"></a>  check(3, false, [3]);
<a name="line921"></a>  check(1, true, [3, 1]);
<a name="line922"></a>  check(5, true, [5, 3, 1]);
<a name="line923"></a>  check(2, true, [5, 3, 2, 1]);
<a name="line924"></a>  check(2, false, [5, 3, 2, 1]);
<a name="line925"></a>
<a name="line926"></a>  check = makeChecker(a, goog.array.binaryRemove, revNumCompare);
<a name="line927"></a>  check(0, false, [5, 3, 2, 1]);
<a name="line928"></a>  check(3, true, [5, 2, 1]);
<a name="line929"></a>  check(1, true, [5, 2]);
<a name="line930"></a>  check(5, true, [2]);
<a name="line931"></a>  check(2, true, []);
<a name="line932"></a>  check(2, false, []);
<a name="line933"></a>}
<a name="line934"></a>
<a name="line935"></a>function testBinarySearch() {
<a name="line936"></a>  var insertionPoint = function(position) { return -(position + 1)};
<a name="line937"></a>  var pos;
<a name="line938"></a>
<a name="line939"></a>  // test default comparison on array of String(s)
<a name="line940"></a>  var a = [&#39;1000&#39;, &#39;9&#39;, &#39;AB&#39;, &#39;ABC&#39;, &#39;ABCABC&#39;, &#39;ABD&#39;, &#39;ABDA&#39;, &#39;B&#39;, &#39;B&#39;, &#39;B&#39;,
<a name="line941"></a>           &#39;C&#39;, &#39;CA&#39;, &#39;CC&#39;, &#39;ZZZ&#39;, &#39;ab&#39;, &#39;abc&#39;, &#39;abcabc&#39;, &#39;abd&#39;, &#39;abda&#39;, &#39;b&#39;,
<a name="line942"></a>           &#39;c&#39;, &#39;ca&#39;, &#39;cc&#39;, &#39;zzz&#39;];
<a name="line943"></a>
<a name="line944"></a>  assertEquals(&#39;\&#39;1000\&#39; should be found at index 0&#39;, 0,
<a name="line945"></a>               goog.array.binarySearch(a, &#39;1000&#39;));
<a name="line946"></a>  assertEquals(&#39;\&#39;zzz\&#39; should be found at index &#39; + (a.length - 1),
<a name="line947"></a>      a.length - 1, goog.array.binarySearch(a, &#39;zzz&#39;));
<a name="line948"></a>  assertEquals(&#39;\&#39;C\&#39; should be found at index 10&#39;, 10,
<a name="line949"></a>               goog.array.binarySearch(a, &#39;C&#39;));
<a name="line950"></a>  pos = goog.array.binarySearch(a, &#39;B&#39;);
<a name="line951"></a>  assertTrue(&#39;\&#39;B\&#39; should be found at index 7 || 8 || 9&#39;, pos == 7 ||
<a name="line952"></a>      pos == 8 || pos == 9);
<a name="line953"></a>  pos = goog.array.binarySearch(a, &#39;100&#39;);
<a name="line954"></a>  assertTrue(&#39;\&#39;100\&#39; should not be found&#39;, pos &lt; 0);
<a name="line955"></a>  assertEquals(&#39;\&#39;100\&#39; should have an insertion point of 0&#39;, 0,
<a name="line956"></a>      insertionPoint(pos));
<a name="line957"></a>  pos = goog.array.binarySearch(a, &#39;zzz0&#39;);
<a name="line958"></a>  assertTrue(&#39;\&#39;zzz0\&#39; should not be found&#39;, pos &lt; 0);
<a name="line959"></a>  assertEquals(&#39;\&#39;zzz0\&#39; should have an insertion point of &#39; + (a.length),
<a name="line960"></a>      a.length, insertionPoint(pos));
<a name="line961"></a>  pos = goog.array.binarySearch(a, &#39;BA&#39;);
<a name="line962"></a>  assertTrue(&#39;\&#39;BA\&#39; should not be found&#39;, pos &lt; 0);
<a name="line963"></a>  assertEquals(&#39;\&#39;BA\&#39; should have an insertion point of 10&#39;, 10,
<a name="line964"></a>      insertionPoint(pos));
<a name="line965"></a>
<a name="line966"></a>  // test 0 length array with default comparison
<a name="line967"></a>  var b = [];
<a name="line968"></a>
<a name="line969"></a>  pos = goog.array.binarySearch(b, &#39;a&#39;);
<a name="line970"></a>  assertTrue(&#39;\&#39;a\&#39; should not be found&#39;, pos &lt; 0);
<a name="line971"></a>  assertEquals(&#39;\&#39;a\&#39; should have an insertion point of 0&#39;, 0,
<a name="line972"></a>      insertionPoint(pos));
<a name="line973"></a>
<a name="line974"></a>  // test single element array with default lexiographical comparison
<a name="line975"></a>  var c = [&#39;only item&#39;];
<a name="line976"></a>
<a name="line977"></a>  assertEquals(&#39;\&#39;only item\&#39; should be found at index 0&#39;, 0,
<a name="line978"></a>      goog.array.binarySearch(c, &#39;only item&#39;));
<a name="line979"></a>  pos = goog.array.binarySearch(c, &#39;a&#39;);
<a name="line980"></a>  assertTrue(&#39;\&#39;a\&#39; should not be found&#39;, pos &lt; 0);
<a name="line981"></a>  assertEquals(&#39;\&#39;a\&#39; should have an insertion point of 0&#39;, 0,
<a name="line982"></a>      insertionPoint(pos));
<a name="line983"></a>  pos = goog.array.binarySearch(c, &#39;z&#39;);
<a name="line984"></a>  assertTrue(&#39;\&#39;z\&#39; should not be found&#39;, pos &lt; 0);
<a name="line985"></a>  assertEquals(&#39;\&#39;z\&#39; should have an insertion point of 1&#39;, 1,
<a name="line986"></a>      insertionPoint(pos));
<a name="line987"></a>
<a name="line988"></a>  // test default comparison on array of Number(s)
<a name="line989"></a>  var d = [-897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0, 0,
<a name="line990"></a>           0.31255, 5, 142.88888708, 334, 342, 453, 54254];
<a name="line991"></a>
<a name="line992"></a>  assertEquals(&#39;-897123.9 should be found at index 0&#39;, 0,
<a name="line993"></a>      goog.array.binarySearch(d, -897123.9));
<a name="line994"></a>  assertEquals(&#39;54254 should be found at index &#39; + (a.length - 1),
<a name="line995"></a>      d.length - 1, goog.array.binarySearch(d, 54254));
<a name="line996"></a>  assertEquals(
<a name="line997"></a>      &#39;-3 should be found at index 5&#39;, 5, goog.array.binarySearch(d, -3));
<a name="line998"></a>  pos = goog.array.binarySearch(d, 0);
<a name="line999"></a>  assertTrue(&#39;0 should be found at index 6 || 7 || 8&#39;, pos == 6 ||
<a name="line1000"></a>      pos == 7 || pos == 8);
<a name="line1001"></a>  pos = goog.array.binarySearch(d, -900000);
<a name="line1002"></a>  assertTrue(&#39;-900000 should not be found&#39;, pos &lt; 0);
<a name="line1003"></a>  assertEquals(&#39;-900000 should have an insertion point of 0&#39;, 0,
<a name="line1004"></a>      insertionPoint(pos));
<a name="line1005"></a>  pos = goog.array.binarySearch(d, &#39;54255&#39;);
<a name="line1006"></a>  assertTrue(&#39;54255 should not be found&#39;, pos &lt; 0);
<a name="line1007"></a>  assertEquals(&#39;54255 should have an insertion point of &#39; + (d.length),
<a name="line1008"></a>      d.length, insertionPoint(pos));
<a name="line1009"></a>  pos = goog.array.binarySearch(d, 1.1);
<a name="line1010"></a>  assertTrue(&#39;1.1 should not be found&#39;, pos &lt; 0);
<a name="line1011"></a>  assertEquals(&#39;1.1 should have an insertion point of 10&#39;, 10,
<a name="line1012"></a>      insertionPoint(pos));
<a name="line1013"></a>
<a name="line1014"></a>  // test with custom comparison function, which reverse orders numbers
<a name="line1015"></a>  var revNumCompare = function(a, b) { return b - a; };
<a name="line1016"></a>
<a name="line1017"></a>  var e = [54254, 453, 342, 334, 142.88888708, 5, 0.31255, 0, 0, 0, -3,
<a name="line1018"></a>           -9, -324, -1321.3124, -321434.58758, -897123.9];
<a name="line1019"></a>
<a name="line1020"></a>  assertEquals(&#39;54254 should be found at index 0&#39;, 0,
<a name="line1021"></a>               goog.array.binarySearch(e, 54254, revNumCompare));
<a name="line1022"></a>  assertEquals(&#39;-897123.9 should be found at index &#39; + (e.length - 1),
<a name="line1023"></a>      e.length - 1, goog.array.binarySearch(e, -897123.9, revNumCompare));
<a name="line1024"></a>  assertEquals(&#39;-3 should be found at index 10&#39;, 10,
<a name="line1025"></a>               goog.array.binarySearch(e, -3, revNumCompare));
<a name="line1026"></a>  pos = goog.array.binarySearch(e, 0, revNumCompare);
<a name="line1027"></a>  assertTrue(&#39;0 should be found at index  || 10&#39;, pos == 7 ||
<a name="line1028"></a>      pos == 9 || pos == 10);
<a name="line1029"></a>  pos = goog.array.binarySearch(e, 54254.1, revNumCompare);
<a name="line1030"></a>  assertTrue(&#39;54254.1 should not be found&#39;, pos &lt; 0);
<a name="line1031"></a>  assertEquals(&#39;54254.1 should have an insertion point of 0&#39;, 0,
<a name="line1032"></a>      insertionPoint(pos));
<a name="line1033"></a>  pos = goog.array.binarySearch(e, -897124, revNumCompare);
<a name="line1034"></a>  assertTrue(&#39;-897124 should not be found&#39;, pos &lt; 0);
<a name="line1035"></a>  assertEquals(&#39;-897124 should have an insertion point of &#39; + (e.length),
<a name="line1036"></a>      e.length, insertionPoint(pos));
<a name="line1037"></a>  pos = goog.array.binarySearch(e, 1.1, revNumCompare);
<a name="line1038"></a>  assertTrue(&#39;1.1 should not be found&#39;, pos &lt; 0);
<a name="line1039"></a>  assertEquals(&#39;1.1 should have an insertion point of 6&#39;, 6,
<a name="line1040"></a>      insertionPoint(pos));
<a name="line1041"></a>
<a name="line1042"></a>  // test 0 length array with custom comparison function
<a name="line1043"></a>  var f = [];
<a name="line1044"></a>
<a name="line1045"></a>  pos = goog.array.binarySearch(f, 0, revNumCompare);
<a name="line1046"></a>  assertTrue(&#39;0 should not be found&#39;, pos &lt; 0);
<a name="line1047"></a>  assertEquals(&#39;0 should have an insertion point of 0&#39;, 0,
<a name="line1048"></a>      insertionPoint(pos));
<a name="line1049"></a>
<a name="line1050"></a>  // test single element array with custom comparison function
<a name="line1051"></a>  var g = [1];
<a name="line1052"></a>
<a name="line1053"></a>  assertEquals(&#39;1 should be found at index 0&#39;, 0, goog.array.binarySearch(g, 1,
<a name="line1054"></a>      revNumCompare));
<a name="line1055"></a>  pos = goog.array.binarySearch(g, 2, revNumCompare);
<a name="line1056"></a>  assertTrue(&#39;2 should not be found&#39;, pos &lt; 0);
<a name="line1057"></a>  assertEquals(&#39;2 should have an insertion point of 0&#39;, 0,
<a name="line1058"></a>      insertionPoint(pos));
<a name="line1059"></a>  pos = goog.array.binarySearch(g, 0, revNumCompare);
<a name="line1060"></a>  assertTrue(&#39;0 should not be found&#39;, pos &lt; 0);
<a name="line1061"></a>  assertEquals(&#39;0 should have an insertion point of 1&#39;, 1,
<a name="line1062"></a>      insertionPoint(pos));
<a name="line1063"></a>
<a name="line1064"></a>  assertEquals(&#39;binarySearch should find the index of the first 0&#39;,
<a name="line1065"></a>      0, goog.array.binarySearch([0, 0, 1], 0));
<a name="line1066"></a>  assertEquals(&#39;binarySearch should find the index of the first 1&#39;,
<a name="line1067"></a>      1, goog.array.binarySearch([0, 1, 1], 1));
<a name="line1068"></a>}
<a name="line1069"></a>
<a name="line1070"></a>
<a name="line1071"></a>function testBinarySearchPerformance() {
<a name="line1072"></a>  // Ensure that Array#slice, Function#apply and Function#call are not called
<a name="line1073"></a>  // from within binarySearch, since they have performance implications in IE.
<a name="line1074"></a>
<a name="line1075"></a>  var propertyReplacer = new goog.testing.PropertyReplacer();
<a name="line1076"></a>  propertyReplacer.replace(Array.prototype, &#39;slice&#39;, function() {
<a name="line1077"></a>    fail(&#39;Should not call Array#slice from binary search.&#39;);
<a name="line1078"></a>  });
<a name="line1079"></a>  propertyReplacer.replace(Function.prototype, &#39;apply&#39;, function() {
<a name="line1080"></a>    fail(&#39;Should not call Function#apply from binary search.&#39;);
<a name="line1081"></a>  });
<a name="line1082"></a>  propertyReplacer.replace(Function.prototype, &#39;call&#39;, function() {
<a name="line1083"></a>    fail(&#39;Should not call Function#call from binary search.&#39;);
<a name="line1084"></a>  });
<a name="line1085"></a>
<a name="line1086"></a>  try {
<a name="line1087"></a>    var array = [1, 5, 7, 11, 13, 16, 19, 24, 28, 31, 33, 36, 40, 50, 52, 55];
<a name="line1088"></a>    // Test with the default comparison function.
<a name="line1089"></a>    goog.array.binarySearch(array, 48);
<a name="line1090"></a>    // Test with a custom comparison function.
<a name="line1091"></a>    goog.array.binarySearch(array, 13, function(a, b) {
<a name="line1092"></a>      return a &gt; b ? 1 : a &lt; b ? -1 : 0;
<a name="line1093"></a>    });
<a name="line1094"></a>  } finally {
<a name="line1095"></a>    // The test runner uses Function.prototype.apply to call tearDown in the
<a name="line1096"></a>    // global context so it has to be reset here.
<a name="line1097"></a>    propertyReplacer.reset();
<a name="line1098"></a>  }
<a name="line1099"></a>}
<a name="line1100"></a>
<a name="line1101"></a>
<a name="line1102"></a>function testBinarySelect() {
<a name="line1103"></a>  var insertionPoint = function(position) { return -(position + 1)};
<a name="line1104"></a>  var numbers = [-897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0, 0, 0,
<a name="line1105"></a>                 0.31255, 5, 142.88888708, 334, 342, 453, 54254];
<a name="line1106"></a>  var objects = goog.array.map(numbers, function(n) {return {n: n};});
<a name="line1107"></a>  function makeEvaluator(target) {
<a name="line1108"></a>    return function(obj, i, arr) {
<a name="line1109"></a>      assertEquals(objects, arr);
<a name="line1110"></a>      assertEquals(obj, arr[i]);
<a name="line1111"></a>      return target - obj.n;
<a name="line1112"></a>    };
<a name="line1113"></a>  }
<a name="line1114"></a>  assertEquals(&#39;{n:-897123.9} should be found at index 0&#39;, 0,
<a name="line1115"></a>      goog.array.binarySelect(objects, makeEvaluator(-897123.9)));
<a name="line1116"></a>  assertEquals(&#39;{n:54254} should be found at index &#39; + (objects.length - 1),
<a name="line1117"></a>      objects.length - 1,
<a name="line1118"></a>      goog.array.binarySelect(objects, makeEvaluator(54254)));
<a name="line1119"></a>  assertEquals(&#39;{n:-3} should be found at index 5&#39;, 5,
<a name="line1120"></a>      goog.array.binarySelect(objects, makeEvaluator(-3)));
<a name="line1121"></a>  pos = goog.array.binarySelect(objects, makeEvaluator(0));
<a name="line1122"></a>  assertTrue(&#39;{n:0} should be found at index 6 || 7 || 8&#39;, pos == 6 ||
<a name="line1123"></a>      pos == 7 || pos == 8);
<a name="line1124"></a>  pos = goog.array.binarySelect(objects, makeEvaluator(-900000));
<a name="line1125"></a>  assertTrue(&#39;{n:-900000} should not be found&#39;, pos &lt; 0);
<a name="line1126"></a>  assertEquals(&#39;{n:-900000} should have an insertion point of 0&#39;, 0,
<a name="line1127"></a>      insertionPoint(pos));
<a name="line1128"></a>  pos = goog.array.binarySelect(objects, makeEvaluator(&#39;54255&#39;));
<a name="line1129"></a>  assertTrue(&#39;{n:54255} should not be found&#39;, pos &lt; 0);
<a name="line1130"></a>  assertEquals(&#39;{n:54255} should have an insertion point of &#39; +
<a name="line1131"></a>      (objects.length), objects.length, insertionPoint(pos));
<a name="line1132"></a>  pos = goog.array.binarySelect(objects, makeEvaluator(1.1));
<a name="line1133"></a>  assertTrue(&#39;{n:1.1} should not be found&#39;, pos &lt; 0);
<a name="line1134"></a>  assertEquals(&#39;{n:1.1} should have an insertion point of 10&#39;, 10,
<a name="line1135"></a>      insertionPoint(pos));
<a name="line1136"></a>}
<a name="line1137"></a>
<a name="line1138"></a>
<a name="line1139"></a>function testArrayEquals() {
<a name="line1140"></a>  // Test argument types.
<a name="line1141"></a>  assertFalse(&#39;array == not array&#39;, goog.array.equals([], null));
<a name="line1142"></a>  assertFalse(&#39;not array == array&#39;, goog.array.equals(null, []));
<a name="line1143"></a>  assertFalse(&#39;not array == not array&#39;, goog.array.equals(null, null));
<a name="line1144"></a>
<a name="line1145"></a>  // Test with default comparison function.
<a name="line1146"></a>  assertTrue(&#39;[] == []&#39;, goog.array.equals([], []));
<a name="line1147"></a>  assertTrue(&#39;[1] == [1]&#39;, goog.array.equals([1], [1]));
<a name="line1148"></a>  assertTrue(&#39;[&quot;1&quot;] == [&quot;1&quot;]&#39;, goog.array.equals([&#39;1&#39;], [&#39;1&#39;]));
<a name="line1149"></a>  assertFalse(&#39;[1] == [&quot;1&quot;]&#39;, goog.array.equals([1], [&#39;1&#39;]));
<a name="line1150"></a>  assertTrue(&#39;[null] == [null]&#39;, goog.array.equals([null], [null]));
<a name="line1151"></a>  assertFalse(&#39;[null] == [undefined]&#39;, goog.array.equals([null], [undefined]));
<a name="line1152"></a>  assertTrue(&#39;[1, 2] == [1, 2]&#39;, goog.array.equals([1, 2], [1, 2]));
<a name="line1153"></a>  assertFalse(&#39;[1, 2] == [2, 1]&#39;, goog.array.equals([1, 2], [2, 1]));
<a name="line1154"></a>  assertFalse(&#39;[1, 2] == [1]&#39;, goog.array.equals([1, 2], [1]));
<a name="line1155"></a>  assertFalse(&#39;[1] == [1, 2]&#39;, goog.array.equals([1], [1, 2]));
<a name="line1156"></a>  assertFalse(&#39;[{}] == [{}]&#39;, goog.array.equals([{}], [{}]));
<a name="line1157"></a>
<a name="line1158"></a>  // Test with custom comparison function.
<a name="line1159"></a>  var cmp = function(a, b) { return typeof a == typeof b; };
<a name="line1160"></a>  assertTrue(&#39;[] cmp []&#39;, goog.array.equals([], [], cmp));
<a name="line1161"></a>  assertTrue(&#39;[1] cmp [1]&#39;, goog.array.equals([1], [1], cmp));
<a name="line1162"></a>  assertTrue(&#39;[1] cmp [2]&#39;, goog.array.equals([1], [2], cmp));
<a name="line1163"></a>  assertTrue(&#39;[&quot;1&quot;] cmp [&quot;1&quot;]&#39;, goog.array.equals([&#39;1&#39;], [&#39;1&#39;], cmp));
<a name="line1164"></a>  assertTrue(&#39;[&quot;1&quot;] cmp [&quot;2&quot;]&#39;, goog.array.equals([&#39;1&#39;], [&#39;2&#39;], cmp));
<a name="line1165"></a>  assertFalse(&#39;[1] cmp [&quot;1&quot;]&#39;, goog.array.equals([1], [&#39;1&#39;], cmp));
<a name="line1166"></a>  assertTrue(&#39;[1, 2] cmp [3, 4]&#39;, goog.array.equals([1, 2], [3, 4], cmp));
<a name="line1167"></a>  assertFalse(&#39;[1] cmp [2, 3]&#39;, goog.array.equals([1], [2, 3], cmp));
<a name="line1168"></a>  assertTrue(&#39;[{}] cmp [{}]&#39;, goog.array.equals([{}], [{}], cmp));
<a name="line1169"></a>  assertTrue(&#39;[{}] cmp [{a: 1}]&#39;, goog.array.equals([{}], [{a: 1}], cmp));
<a name="line1170"></a>
<a name="line1171"></a>  // Test with array-like objects.
<a name="line1172"></a>  assertTrue(&#39;[5] == obj [5]&#39;,
<a name="line1173"></a>      goog.array.equals([5], {0: 5, length: 1}));
<a name="line1174"></a>  assertTrue(&#39;obj [5] == [5]&#39;,
<a name="line1175"></a>      goog.array.equals({0: 5, length: 1}, [5]));
<a name="line1176"></a>  assertTrue(&#39;[&quot;x&quot;] == obj [&quot;x&quot;]&#39;,
<a name="line1177"></a>      goog.array.equals([&#39;x&#39;], {0: &#39;x&#39;, length: 1}));
<a name="line1178"></a>  assertTrue(&#39;obj [&quot;x&quot;] == [&quot;x&quot;]&#39;,
<a name="line1179"></a>      goog.array.equals({0: &#39;x&#39;, length: 1}, [&#39;x&#39;]));
<a name="line1180"></a>  assertTrue(&#39;[5] == {0: 5, 1: 6, length: 1}&#39;,
<a name="line1181"></a>      goog.array.equals([5], {0: 5, 1: 6, length: 1}));
<a name="line1182"></a>  assertTrue(&#39;{0: 5, 1: 6, length: 1} == [5]&#39;,
<a name="line1183"></a>      goog.array.equals({0: 5, 1: 6, length: 1}, [5]));
<a name="line1184"></a>  assertFalse(&#39;[5, 6] == {0: 5, 1: 6, length: 1}&#39;,
<a name="line1185"></a>      goog.array.equals([5, 6], {0: 5, 1: 6, length: 1}));
<a name="line1186"></a>  assertFalse(&#39;{0: 5, 1: 6, length: 1}, [5, 6]&#39;,
<a name="line1187"></a>      goog.array.equals({0: 5, 1: 6, length: 1}, [5, 6]));
<a name="line1188"></a>  assertTrue(&#39;[5, 6] == obj [5, 6]&#39;,
<a name="line1189"></a>      goog.array.equals([5, 6], {0: 5, 1: 6, length: 2}));
<a name="line1190"></a>  assertTrue(&#39;obj [5, 6] == [5, 6]&#39;,
<a name="line1191"></a>      goog.array.equals({0: 5, 1: 6, length: 2}, [5, 6]));
<a name="line1192"></a>  assertFalse(&#39;{0: 5, 1: 6} == [5, 6]&#39;,
<a name="line1193"></a>      goog.array.equals({0: 5, 1: 6}, [5, 6]));
<a name="line1194"></a>}
<a name="line1195"></a>
<a name="line1196"></a>
<a name="line1197"></a>function testArrayCompare3Basic() {
<a name="line1198"></a>  assertEquals(0, goog.array.compare3([], []));
<a name="line1199"></a>  assertEquals(0, goog.array.compare3([&#39;111&#39;, &#39;222&#39;], [&#39;111&#39;, &#39;222&#39;]));
<a name="line1200"></a>  assertEquals(-1, goog.array.compare3([&#39;111&#39;, &#39;222&#39;], [&#39;1111&#39;, &#39;&#39;]));
<a name="line1201"></a>  assertEquals(1, goog.array.compare3([&#39;111&#39;, &#39;222&#39;], [&#39;111&#39;]));
<a name="line1202"></a>  assertEquals(1, goog.array.compare3([&#39;11&#39;, &#39;222&#39;, &#39;333&#39;], []));
<a name="line1203"></a>  assertEquals(-1, goog.array.compare3([], [&#39;11&#39;, &#39;222&#39;, &#39;333&#39;]));
<a name="line1204"></a>}
<a name="line1205"></a>
<a name="line1206"></a>
<a name="line1207"></a>function testArrayCompare3ComparatorFn() {
<a name="line1208"></a>  function cmp(a, b) {
<a name="line1209"></a>    return a - b;
<a name="line1210"></a>  };
<a name="line1211"></a>  assertEquals(0, goog.array.compare3([], [], cmp));
<a name="line1212"></a>  assertEquals(0, goog.array.compare3([8, 4], [8, 4], cmp));
<a name="line1213"></a>  assertEquals(-1, goog.array.compare3([4, 3], [5, 0]));
<a name="line1214"></a>  assertEquals(1, goog.array.compare3([6, 2], [6]));
<a name="line1215"></a>  assertEquals(1, goog.array.compare3([1, 2, 3], []));
<a name="line1216"></a>  assertEquals(-1, goog.array.compare3([], [1, 2, 3]));
<a name="line1217"></a>}
<a name="line1218"></a>
<a name="line1219"></a>
<a name="line1220"></a>function testSort() {
<a name="line1221"></a>  // Test sorting empty array
<a name="line1222"></a>  var a = [];
<a name="line1223"></a>  goog.array.sort(a);
<a name="line1224"></a>  assertEquals(&#39;Sorted empty array is still an empty array (length 0)&#39;, 0,
<a name="line1225"></a>      a.length);
<a name="line1226"></a>
<a name="line1227"></a>  // Test sorting homogenous array of String(s) of length &gt; 1
<a name="line1228"></a>  var b = [&#39;JUST&#39;, &#39;1&#39;, &#39;test&#39;, &#39;Array&#39;, &#39;to&#39;, &#39;test&#39;, &#39;array&#39;, &#39;Sort&#39;,
<a name="line1229"></a>           &#39;about&#39;, &#39;NOW&#39;, &#39;!!&#39;];
<a name="line1230"></a>  var bSorted = [&#39;!!&#39;, &#39;1&#39;, &#39;Array&#39;, &#39;JUST&#39;, &#39;NOW&#39;, &#39;Sort&#39;, &#39;about&#39;, &#39;array&#39;,
<a name="line1231"></a>                 &#39;test&#39;, &#39;test&#39;, &#39;to&#39;];
<a name="line1232"></a>  goog.array.sort(b);
<a name="line1233"></a>  assertArrayEquals(bSorted, b);
<a name="line1234"></a>
<a name="line1235"></a>  // Test sorting already sorted array of String(s) of length &gt; 1
<a name="line1236"></a>  goog.array.sort(b);
<a name="line1237"></a>  assertArrayEquals(bSorted, b);
<a name="line1238"></a>
<a name="line1239"></a>  // Test sorting homogenous array of integer Number(s) of length &gt; 1
<a name="line1240"></a>  var c = [100, 1, 2000, -1, 0, 1000023, 12312512, -12331, 123, 54325,
<a name="line1241"></a>           -38104783, 93708, 908, -213, -4, 5423, 0];
<a name="line1242"></a>  var cSorted = [-38104783, -12331, -213, -4, -1, 0, 0, 1, 100, 123, 908,
<a name="line1243"></a>                 2000, 5423, 54325, 93708, 1000023, 12312512];
<a name="line1244"></a>  goog.array.sort(c);
<a name="line1245"></a>  assertArrayEquals(cSorted, c);
<a name="line1246"></a>
<a name="line1247"></a>  // Test sorting already sorted array of integer Number(s) of length &gt; 1
<a name="line1248"></a>  goog.array.sort(c);
<a name="line1249"></a>  assertArrayEquals(cSorted, c);
<a name="line1250"></a>
<a name="line1251"></a>  // Test sorting homogenous array of Number(s) of length &gt; 1
<a name="line1252"></a>  var e = [-1321.3124, 0.31255, 54254, 0, 142.88888708, -321434.58758, -324,
<a name="line1253"></a>           453, 334, -3, 5, -9, 342, -897123.9];
<a name="line1254"></a>  var eSorted = [-897123.9, -321434.58758, -1321.3124, -324, -9, -3, 0,
<a name="line1255"></a>                 0.31255, 5, 142.88888708, 334, 342, 453, 54254];
<a name="line1256"></a>  goog.array.sort(e);
<a name="line1257"></a>  assertArrayEquals(eSorted, e);
<a name="line1258"></a>
<a name="line1259"></a>  // Test sorting already sorted array of Number(s) of length &gt; 1
<a name="line1260"></a>  goog.array.sort(e);
<a name="line1261"></a>  assertArrayEquals(eSorted, e);
<a name="line1262"></a>
<a name="line1263"></a>  // Test sorting array of Number(s) of length &gt; 1,
<a name="line1264"></a>  // using custom comparison function which does reverse ordering
<a name="line1265"></a>  var f = [-1321.3124, 0.31255, 54254, 0, 142.88888708, -321434.58758,
<a name="line1266"></a>           -324, 453, 334, -3, 5, -9, 342, -897123.9];
<a name="line1267"></a>  var fSorted = [54254, 453, 342, 334, 142.88888708, 5, 0.31255, 0, -3, -9,
<a name="line1268"></a>                 -324, -1321.3124, -321434.58758, -897123.9];
<a name="line1269"></a>  goog.array.sort(f, function(a, b) { return b - a; });
<a name="line1270"></a>  assertArrayEquals(fSorted, f);
<a name="line1271"></a>
<a name="line1272"></a>  // Test sorting already sorted array of Number(s) of length &gt; 1
<a name="line1273"></a>  // using custom comparison function which does reverse ordering
<a name="line1274"></a>  goog.array.sort(f, function(a, b) {return b - a; });
<a name="line1275"></a>  assertArrayEquals(fSorted, f);
<a name="line1276"></a>
<a name="line1277"></a>  // Test sorting array of custom Object(s) of length &gt; 1 that have
<a name="line1278"></a>  // an overriden toString
<a name="line1279"></a>  function ComparedObject(value) {
<a name="line1280"></a>    this.value = value;
<a name="line1281"></a>  };
<a name="line1282"></a>
<a name="line1283"></a>  ComparedObject.prototype.toString = function() {
<a name="line1284"></a>    return this.value;
<a name="line1285"></a>  };
<a name="line1286"></a>
<a name="line1287"></a>  var co1 = new ComparedObject(&#39;a&#39;);
<a name="line1288"></a>  var co2 = new ComparedObject(&#39;b&#39;);
<a name="line1289"></a>  var co3 = new ComparedObject(&#39;c&#39;);
<a name="line1290"></a>  var co4 = new ComparedObject(&#39;d&#39;);
<a name="line1291"></a>
<a name="line1292"></a>  var g = [co3, co4, co2, co1];
<a name="line1293"></a>  var gSorted = [co1, co2, co3, co4];
<a name="line1294"></a>  goog.array.sort(g);
<a name="line1295"></a>  assertArrayEquals(gSorted, g);
<a name="line1296"></a>
<a name="line1297"></a>  // Test sorting already sorted array of custom Object(s) of length &gt; 1
<a name="line1298"></a>  // that have an overriden toString
<a name="line1299"></a>  goog.array.sort(g);
<a name="line1300"></a>  assertArrayEquals(gSorted, g);
<a name="line1301"></a>
<a name="line1302"></a>  // Test sorting an array of custom Object(s) of length &gt; 1 using
<a name="line1303"></a>  // a custom comparison function
<a name="line1304"></a>  var h = [co4, co2, co1, co3];
<a name="line1305"></a>  var hSorted = [co1, co2, co3, co4];
<a name="line1306"></a>  goog.array.sort(h, function(a, b) {
<a name="line1307"></a>    return a.value &gt; b.value ? 1 : a.value &lt; b.value ? -1 : 0;
<a name="line1308"></a>  });
<a name="line1309"></a>  assertArrayEquals(hSorted, h);
<a name="line1310"></a>
<a name="line1311"></a>  // Test sorting already sorted array of custom Object(s) of length &gt; 1
<a name="line1312"></a>  // using a custom comparison function
<a name="line1313"></a>  goog.array.sort(h);
<a name="line1314"></a>  assertArrayEquals(hSorted, h);
<a name="line1315"></a>
<a name="line1316"></a>  // Test sorting arrays of length 1
<a name="line1317"></a>  var i = [&#39;one&#39;];
<a name="line1318"></a>  var iSorted = [&#39;one&#39;];
<a name="line1319"></a>  goog.array.sort(i);
<a name="line1320"></a>  assertArrayEquals(iSorted, i);
<a name="line1321"></a>
<a name="line1322"></a>  var j = [1];
<a name="line1323"></a>  var jSorted = [1];
<a name="line1324"></a>  goog.array.sort(j);
<a name="line1325"></a>  assertArrayEquals(jSorted, j);
<a name="line1326"></a>
<a name="line1327"></a>  var k = [1.1];
<a name="line1328"></a>  var kSorted = [1.1];
<a name="line1329"></a>  goog.array.sort(k);
<a name="line1330"></a>  assertArrayEquals(kSorted, k);
<a name="line1331"></a>
<a name="line1332"></a>  var l = [co3];
<a name="line1333"></a>  var lSorted = [co3];
<a name="line1334"></a>  goog.array.sort(l);
<a name="line1335"></a>  assertArrayEquals(lSorted, l);
<a name="line1336"></a>
<a name="line1337"></a>  var m = [co2];
<a name="line1338"></a>  var mSorted = [co2];
<a name="line1339"></a>  goog.array.sort(m, function(a, b) {
<a name="line1340"></a>    return a.value &gt; b.value ? 1 : a.value &lt; b.value ? -1 : 0;
<a name="line1341"></a>  });
<a name="line1342"></a>  assertArrayEquals(mSorted, m);
<a name="line1343"></a>}
<a name="line1344"></a>
<a name="line1345"></a>function testStableSort() {
<a name="line1346"></a>  // Test array with custom comparison function
<a name="line1347"></a>  var arr = [{key: 3, val: &#39;a&#39;}, {key: 2, val: &#39;b&#39;}, {key: 3, val: &#39;c&#39;},
<a name="line1348"></a>             {key: 4, val: &#39;d&#39;}, {key: 3, val: &#39;e&#39;}];
<a name="line1349"></a>  var arrClone = goog.array.clone(arr);
<a name="line1350"></a>
<a name="line1351"></a>  function comparisonFn(obj1, obj2) {
<a name="line1352"></a>    return obj1.key - obj2.key;
<a name="line1353"></a>  }
<a name="line1354"></a>  goog.array.stableSort(arr, comparisonFn);
<a name="line1355"></a>  var sortedValues = [];
<a name="line1356"></a>  for (var i = 0; i &lt; arr.length; i++) {
<a name="line1357"></a>    sortedValues.push(arr[i].val);
<a name="line1358"></a>  }
<a name="line1359"></a>  var wantedSortedValues = [&#39;b&#39;, &#39;a&#39;, &#39;c&#39;, &#39;e&#39;, &#39;d&#39;];
<a name="line1360"></a>  assertArrayEquals(wantedSortedValues, sortedValues);
<a name="line1361"></a>
<a name="line1362"></a>  // Test array without custom comparison function
<a name="line1363"></a>  var arr2 = [];
<a name="line1364"></a>  for (var i = 0; i &lt; arrClone.length; i++) {
<a name="line1365"></a>    arr2.push({val: arrClone[i].val, toString:
<a name="line1366"></a>          goog.partial(function(index) {
<a name="line1367"></a>            return arrClone[index].key;
<a name="line1368"></a>          }, i)});
<a name="line1369"></a>  }
<a name="line1370"></a>  goog.array.stableSort(arr2);
<a name="line1371"></a>  var sortedValues2 = [];
<a name="line1372"></a>  for (var i = 0; i &lt; arr2.length; i++) {
<a name="line1373"></a>    sortedValues2.push(arr2[i].val);
<a name="line1374"></a>  }
<a name="line1375"></a>  assertArrayEquals(wantedSortedValues, sortedValues2);
<a name="line1376"></a>}
<a name="line1377"></a>
<a name="line1378"></a>function testArrayBucketModulus() {
<a name="line1379"></a>  // bucket things by modulus
<a name="line1380"></a>  var a = {};
<a name="line1381"></a>  var b = [];
<a name="line1382"></a>
<a name="line1383"></a>  function modFive(num) {
<a name="line1384"></a>    return num % 5;
<a name="line1385"></a>  }
<a name="line1386"></a>
<a name="line1387"></a>  for (var i = 0; i &lt; 20; i++) {
<a name="line1388"></a>    var mod = modFive(i);
<a name="line1389"></a>    a[mod] = a[mod] || [];
<a name="line1390"></a>    a[mod].push(i);
<a name="line1391"></a>    b.push(i);
<a name="line1392"></a>  }
<a name="line1393"></a>
<a name="line1394"></a>  var buckets = goog.array.bucket(b, modFive);
<a name="line1395"></a>
<a name="line1396"></a>  for (var i = 0; i &lt; 5; i++) {
<a name="line1397"></a>    // The order isn&#39;t defined, but they should be the same sorted.
<a name="line1398"></a>    goog.array.sort(a[i]);
<a name="line1399"></a>    goog.array.sort(buckets[i]);
<a name="line1400"></a>    assertArrayEquals(a[i], buckets[i]);
<a name="line1401"></a>  }
<a name="line1402"></a>}
<a name="line1403"></a>
<a name="line1404"></a>function testArrayBucketEvenOdd() {
<a name="line1405"></a>  var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
<a name="line1406"></a>
<a name="line1407"></a>  // test even/odd
<a name="line1408"></a>  function isEven(value, index, array) {
<a name="line1409"></a>    assertEquals(value, array[index]);
<a name="line1410"></a>    assertEquals(&#39;number&#39;, typeof index);
<a name="line1411"></a>    assertEquals(a, array);
<a name="line1412"></a>    return value % 2 == 0;
<a name="line1413"></a>  }
<a name="line1414"></a>
<a name="line1415"></a>  var b = goog.array.bucket(a, isEven);
<a name="line1416"></a>
<a name="line1417"></a>  assertArrayEquals(b[true], [2, 4, 6, 8]);
<a name="line1418"></a>  assertArrayEquals(b[false], [1, 3, 5, 7, 9]);
<a name="line1419"></a>}
<a name="line1420"></a>
<a name="line1421"></a>function testArrayBucketUsingThisObject() {
<a name="line1422"></a>  var a = [1, 2, 3, 4, 5];
<a name="line1423"></a>
<a name="line1424"></a>  var obj = {
<a name="line1425"></a>    specialValue: 2
<a name="line1426"></a>  };
<a name="line1427"></a>
<a name="line1428"></a>  function isSpecialValue(value, index, array) {
<a name="line1429"></a>    return value == this.specialValue ? 1 : 0;
<a name="line1430"></a>  }
<a name="line1431"></a>
<a name="line1432"></a>  var b = goog.array.bucket(a, isSpecialValue, obj);
<a name="line1433"></a>  assertArrayEquals(b[0], [1, 3, 4, 5]);
<a name="line1434"></a>  assertArrayEquals(b[1], [2]);
<a name="line1435"></a>}
<a name="line1436"></a>
<a name="line1437"></a>function testArrayToObject() {
<a name="line1438"></a>  var a = [
<a name="line1439"></a>    {name: &#39;a&#39;},
<a name="line1440"></a>    {name: &#39;b&#39;},
<a name="line1441"></a>    {name: &#39;c&#39;},
<a name="line1442"></a>    {name: &#39;d&#39;}];
<a name="line1443"></a>
<a name="line1444"></a>  function getName(value, index, array) {
<a name="line1445"></a>    assertEquals(value, array[index]);
<a name="line1446"></a>    assertEquals(&#39;number&#39;, typeof index);
<a name="line1447"></a>    assertEquals(a, array);
<a name="line1448"></a>    return value.name;
<a name="line1449"></a>  }
<a name="line1450"></a>
<a name="line1451"></a>  var b = goog.array.toObject(a, getName);
<a name="line1452"></a>
<a name="line1453"></a>  for (var i = 0; i &lt; a.length; i++) {
<a name="line1454"></a>    assertEquals(a[i], b[a[i].name]);
<a name="line1455"></a>  }
<a name="line1456"></a>}
<a name="line1457"></a>
<a name="line1458"></a>function testRange() {
<a name="line1459"></a>  assertArrayEquals([], goog.array.range(0));
<a name="line1460"></a>  assertArrayEquals([], goog.array.range(5, 5, 5));
<a name="line1461"></a>  assertArrayEquals([], goog.array.range(-3, -3));
<a name="line1462"></a>  assertArrayEquals([], goog.array.range(10, undefined, -1));
<a name="line1463"></a>  assertArrayEquals([], goog.array.range(8, 0));
<a name="line1464"></a>  assertArrayEquals([], goog.array.range(-5, -10, 3));
<a name="line1465"></a>
<a name="line1466"></a>  assertArrayEquals([0], goog.array.range(1));
<a name="line1467"></a>  assertArrayEquals([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], goog.array.range(10));
<a name="line1468"></a>
<a name="line1469"></a>  assertArrayEquals([1], goog.array.range(1, 2));
<a name="line1470"></a>  assertArrayEquals([-3, -2, -1, 0, 1, 2], goog.array.range(-3, 3));
<a name="line1471"></a>
<a name="line1472"></a>  assertArrayEquals([4], goog.array.range(4, 40, 400));
<a name="line1473"></a>  assertArrayEquals([5, 8, 11, 14], goog.array.range(5, 15, 3));
<a name="line1474"></a>  assertArrayEquals([1, -1, -3], goog.array.range(1, -5, -2));
<a name="line1475"></a>  assertElementsRoughlyEqual([.2, .3, .4], goog.array.range(.2, .5, .1), 0.001);
<a name="line1476"></a>
<a name="line1477"></a>  assertArrayEquals([0], goog.array.range(7, undefined, 9));
<a name="line1478"></a>  assertArrayEquals([0, 2, 4, 6], goog.array.range(8, undefined, 2));
<a name="line1479"></a>}
<a name="line1480"></a>
<a name="line1481"></a>function testArrayRepeat() {
<a name="line1482"></a>  assertArrayEquals([], goog.array.repeat(3, 0));
<a name="line1483"></a>  assertArrayEquals([], goog.array.repeat(3, -1));
<a name="line1484"></a>  assertArrayEquals([3], goog.array.repeat(3, 1));
<a name="line1485"></a>  assertArrayEquals([3, 3, 3], goog.array.repeat(3, 3));
<a name="line1486"></a>  assertArrayEquals([null, null], goog.array.repeat(null, 2));
<a name="line1487"></a>}
<a name="line1488"></a>
<a name="line1489"></a>function testArrayFlatten() {
<a name="line1490"></a>  assertArrayEquals([1, 2, 3, 4, 5], goog.array.flatten(1, 2, 3, 4, 5));
<a name="line1491"></a>  assertArrayEquals([1, 2, 3, 4, 5], goog.array.flatten(1, [2, [3, [4, 5]]]));
<a name="line1492"></a>  assertArrayEquals([1, 2, 3, 4], goog.array.flatten(1, [2, [3, [4]]]));
<a name="line1493"></a>  assertArrayEquals([1, 2, 3, 4], goog.array.flatten([[[1], 2], 3], 4));
<a name="line1494"></a>  assertArrayEquals([1], goog.array.flatten([[1]]));
<a name="line1495"></a>  assertArrayEquals([], goog.array.flatten());
<a name="line1496"></a>  assertArrayEquals([], goog.array.flatten([]));
<a name="line1497"></a>}
<a name="line1498"></a>
<a name="line1499"></a>function testSortObjectsByKey() {
<a name="line1500"></a>  var sortedArray = buildSortedObjectArray(4);
<a name="line1501"></a>  var objects =
<a name="line1502"></a>      [sortedArray[1], sortedArray[2], sortedArray[0], sortedArray[3]];
<a name="line1503"></a>
<a name="line1504"></a>  goog.array.sortObjectsByKey(objects, &#39;name&#39;);
<a name="line1505"></a>  validateObjectArray(sortedArray, objects);
<a name="line1506"></a>}
<a name="line1507"></a>
<a name="line1508"></a>function testSortObjectsByKeyWithCompareFunction() {
<a name="line1509"></a>  var sortedArray = buildSortedObjectArray(4);
<a name="line1510"></a>  var objects =
<a name="line1511"></a>      [sortedArray[1], sortedArray[2], sortedArray[0], sortedArray[3]];
<a name="line1512"></a>  var descSortedArray =
<a name="line1513"></a>      [sortedArray[3], sortedArray[2], sortedArray[1], sortedArray[0]];
<a name="line1514"></a>
<a name="line1515"></a>  function descCompare(a, b) {
<a name="line1516"></a>    return a &lt; b ? 1 : a &gt; b ? -1 : 0;
<a name="line1517"></a>  };
<a name="line1518"></a>
<a name="line1519"></a>  goog.array.sortObjectsByKey(objects, &#39;name&#39;, descCompare);
<a name="line1520"></a>  validateObjectArray(descSortedArray, objects);
<a name="line1521"></a>}
<a name="line1522"></a>
<a name="line1523"></a>function buildSortedObjectArray(size) {
<a name="line1524"></a>  var objectArray = [];
<a name="line1525"></a>  for (var i = 0; i &lt; size; i++) {
<a name="line1526"></a>    objectArray.push({
<a name="line1527"></a>      &#39;name&#39;: &#39;name_&#39; + i,
<a name="line1528"></a>      &#39;id&#39;: &#39;id_&#39; + (size - i)
<a name="line1529"></a>    });
<a name="line1530"></a>  }
<a name="line1531"></a>
<a name="line1532"></a>  return objectArray;
<a name="line1533"></a>}
<a name="line1534"></a>
<a name="line1535"></a>function validateObjectArray(expected, actual) {
<a name="line1536"></a>  assertEquals(expected.length, actual.length);
<a name="line1537"></a>  for (var i = 0; i &lt; expected.length; i++) {
<a name="line1538"></a>    assertEquals(expected[i].name, actual[i].name);
<a name="line1539"></a>    assertEquals(expected[i].id, actual[i].id);
<a name="line1540"></a>  }
<a name="line1541"></a>}
<a name="line1542"></a>
<a name="line1543"></a>function testIsSorted() {
<a name="line1544"></a>  assertTrue(goog.array.isSorted([1, 2, 3]));
<a name="line1545"></a>  assertTrue(goog.array.isSorted([1, 2, 2]));
<a name="line1546"></a>  assertFalse(goog.array.isSorted([1, 2, 1]));
<a name="line1547"></a>
<a name="line1548"></a>  assertTrue(goog.array.isSorted([1, 2, 3], null, true));
<a name="line1549"></a>  assertFalse(goog.array.isSorted([1, 2, 2], null, true));
<a name="line1550"></a>  assertFalse(goog.array.isSorted([1, 2, 1], null, true));
<a name="line1551"></a>
<a name="line1552"></a>  function compare(a, b) {
<a name="line1553"></a>    return b - a;
<a name="line1554"></a>  }
<a name="line1555"></a>
<a name="line1556"></a>  assertFalse(goog.array.isSorted([1, 2, 3], compare));
<a name="line1557"></a>  assertTrue(goog.array.isSorted([3, 2, 2], compare));
<a name="line1558"></a>}
<a name="line1559"></a>
<a name="line1560"></a>function assertRotated(expect, array, rotate) {
<a name="line1561"></a>  assertArrayEquals(expect, goog.array.rotate(array, rotate));
<a name="line1562"></a>}
<a name="line1563"></a>
<a name="line1564"></a>function testRotate() {
<a name="line1565"></a>  assertRotated([], [], 3);
<a name="line1566"></a>  assertRotated([1], [1], 3);
<a name="line1567"></a>  assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], -6);
<a name="line1568"></a>  assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], -5);
<a name="line1569"></a>  assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], -4);
<a name="line1570"></a>  assertRotated([3, 4, 0, 1, 2], [0, 1, 2, 3, 4], -3);
<a name="line1571"></a>  assertRotated([2, 3, 4, 0, 1], [0, 1, 2, 3, 4], -2);
<a name="line1572"></a>  assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], -1);
<a name="line1573"></a>  assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 0);
<a name="line1574"></a>  assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], 1);
<a name="line1575"></a>  assertRotated([3, 4, 0, 1, 2], [0, 1, 2, 3, 4], 2);
<a name="line1576"></a>  assertRotated([2, 3, 4, 0, 1], [0, 1, 2, 3, 4], 3);
<a name="line1577"></a>  assertRotated([1, 2, 3, 4, 0], [0, 1, 2, 3, 4], 4);
<a name="line1578"></a>  assertRotated([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 5);
<a name="line1579"></a>  assertRotated([4, 0, 1, 2, 3], [0, 1, 2, 3, 4], 6);
<a name="line1580"></a>}
<a name="line1581"></a>
<a name="line1582"></a>function testMoveItemWithArray() {
<a name="line1583"></a>  var arr = [0, 1, 2, 3];
<a name="line1584"></a>  goog.array.moveItem(arr, 1, 3);  // toIndex &gt; fromIndex
<a name="line1585"></a>  assertArrayEquals([0, 2, 3, 1], arr);
<a name="line1586"></a>  goog.array.moveItem(arr, 2, 0);  // toIndex &lt; fromIndex
<a name="line1587"></a>  assertArrayEquals([3, 0, 2, 1], arr);
<a name="line1588"></a>  goog.array.moveItem(arr, 1, 1);  // toIndex == fromIndex
<a name="line1589"></a>  assertArrayEquals([3, 0, 2, 1], arr);
<a name="line1590"></a>  // Out-of-bounds indexes throw assertion errors.
<a name="line1591"></a>  assertThrows(function() {
<a name="line1592"></a>    goog.array.moveItem(arr, -1, 1);
<a name="line1593"></a>  });
<a name="line1594"></a>  assertThrows(function() {
<a name="line1595"></a>    goog.array.moveItem(arr, 4, 1);
<a name="line1596"></a>  });
<a name="line1597"></a>  assertThrows(function() {
<a name="line1598"></a>    goog.array.moveItem(arr, 1, -1);
<a name="line1599"></a>  });
<a name="line1600"></a>  assertThrows(function() {
<a name="line1601"></a>    goog.array.moveItem(arr, 1, 4);
<a name="line1602"></a>  });
<a name="line1603"></a>  // The array should not be modified by the out-of-bound calls.
<a name="line1604"></a>  assertArrayEquals([3, 0, 2, 1], arr);
<a name="line1605"></a>}
<a name="line1606"></a>
<a name="line1607"></a>function testMoveItemWithArgumentsObject() {
<a name="line1608"></a>  var f = function() {
<a name="line1609"></a>    goog.array.moveItem(arguments, 0, 1);
<a name="line1610"></a>    return arguments;
<a name="line1611"></a>  };
<a name="line1612"></a>  assertArrayEquals([1, 0], goog.array.toArray(f(0, 1)));
<a name="line1613"></a>}
<a name="line1614"></a>
<a name="line1615"></a>function testConcat() {
<a name="line1616"></a>  var a1 = [1, 2, 3];
<a name="line1617"></a>  var a2 = [4, 5, 6];
<a name="line1618"></a>  var a3 = goog.array.concat(a1, a2);
<a name="line1619"></a>  a1.push(1);
<a name="line1620"></a>  a2.push(5);
<a name="line1621"></a>  assertArrayEquals([1, 2, 3, 4, 5, 6], a3);
<a name="line1622"></a>}
<a name="line1623"></a>
<a name="line1624"></a>function testConcatWithNoSecondArg() {
<a name="line1625"></a>  var a1 = [1, 2, 3, 4];
<a name="line1626"></a>  var a2 = goog.array.concat(a1);
<a name="line1627"></a>  a1.push(5);
<a name="line1628"></a>  assertArrayEquals([1, 2, 3, 4], a2);
<a name="line1629"></a>}
<a name="line1630"></a>
<a name="line1631"></a>function testConcatWithNonArrayArgs() {
<a name="line1632"></a>  var a1 = [1, 2, 3, 4];
<a name="line1633"></a>  var o = {0: &#39;a&#39;, 1: &#39;b&#39;, length: 2};
<a name="line1634"></a>  var a2 = goog.array.concat(a1, 5, &#39;10&#39;, o);
<a name="line1635"></a>  assertArrayEquals([1, 2, 3, 4, 5, &#39;10&#39;, o], a2);
<a name="line1636"></a>}
<a name="line1637"></a>
<a name="line1638"></a>function testConcatWithNull() {
<a name="line1639"></a>  var a1 = goog.array.concat(null, [1, 2, 3]);
<a name="line1640"></a>  var a2 = goog.array.concat([1, 2, 3], null);
<a name="line1641"></a>  assertArrayEquals([null, 1, 2, 3], a1);
<a name="line1642"></a>  assertArrayEquals([1, 2, 3, null], a2);
<a name="line1643"></a>}
<a name="line1644"></a>
<a name="line1645"></a>function testZip() {
<a name="line1646"></a>  var a1 = goog.array.zip([1, 2, 3], [3, 2, 1]);
<a name="line1647"></a>  var a2 = goog.array.zip([1, 2], [3, 2, 1]);
<a name="line1648"></a>  var a3 = goog.array.zip();
<a name="line1649"></a>  assertArrayEquals([[1, 3], [2, 2], [3, 1]], a1);
<a name="line1650"></a>  assertArrayEquals([[1, 3], [2, 2]], a2);
<a name="line1651"></a>  assertArrayEquals([], a3);
<a name="line1652"></a>}
<a name="line1653"></a>
<a name="line1654"></a>function testShuffle() {
<a name="line1655"></a>  // Test array. This array should have unique values for the purposes of this
<a name="line1656"></a>  // test case.
<a name="line1657"></a>  var testArray = [1, 2, 3, 4, 5];
<a name="line1658"></a>  var testArrayCopy = goog.array.clone(testArray);
<a name="line1659"></a>
<a name="line1660"></a>  // Custom random function, which always returns a value approaching 1,
<a name="line1661"></a>  // resulting in a &quot;shuffle&quot; that preserves the order of original array
<a name="line1662"></a>  // (for array sizes that we work with here).
<a name="line1663"></a>  var noChangeShuffleFunction = function() {
<a name="line1664"></a>    return .999999;
<a name="line1665"></a>  };
<a name="line1666"></a>  goog.array.shuffle(testArray, noChangeShuffleFunction);
<a name="line1667"></a>  assertArrayEquals(testArrayCopy, testArray);
<a name="line1668"></a>
<a name="line1669"></a>  // Custom random function, which always returns 0, resulting in a
<a name="line1670"></a>  // deterministic &quot;shuffle&quot; that is predictable but differs from the
<a name="line1671"></a>  // original order of the array.
<a name="line1672"></a>  var testShuffleFunction = function() {
<a name="line1673"></a>    return 0;
<a name="line1674"></a>  };
<a name="line1675"></a>  goog.array.shuffle(testArray, testShuffleFunction);
<a name="line1676"></a>  assertArrayEquals([2, 3, 4, 5, 1], testArray);
<a name="line1677"></a>
<a name="line1678"></a>  // Test the use of a real random function(no optional RNG is specified).
<a name="line1679"></a>  goog.array.shuffle(testArray);
<a name="line1680"></a>
<a name="line1681"></a>  // Ensure the shuffled array comprises the same elements (without regard to
<a name="line1682"></a>  // order).
<a name="line1683"></a>  assertSameElements(testArrayCopy, testArray);
<a name="line1684"></a>}
</pre>


</body>
</html>
